Lesson 1: If/else Reference Error


#1



Ok, i have been trying for 3 days to get this code to work, from what I have learned so far i don't understand why this won't work... I have googled syntax, checked my semi-colons, read posts on here and compared those answers to my code, made changes based on those recommendations, and I still can't get it to work! Am I comparing to things that are impossible to compare (string to number) or something?


I am on this exercise:

https://www.codecademy.com/courses/javascript-beginner-en-qDwp0/0/1?curriculum_id=506324b3a7dffd00020bf661#


When I execute I get this popup:

Oops, try again. It looks like you have a ReferenceError. Maybe you forgot to define a function named isEven?

The console.log shows:

ReferenceError: isEven is not defined
ReferenceError: Invalid left-hand side in assignment


    var myNumber = 0;
    if (myNumber < 1 || myNumber = null) {
        do { 
            myNumber = prompt("Please choose a number: ","1-100");
        } while (myNumber < 1 || myNumber = null);
        console.log("myNumber = " + myNumber);
    };
    var isEven = function(number) {
        if (myNumber % 2 === 0) {
            console.log("True");
            return true;
        } else { 
            console.log("False");
            return false;
        };
    };
    isEven(myNumber);

*Edited names to named


#2

inside your if and while condition, i would compare myNumber with null (using two equal signs) instead of one equal sign, in which case you assign a value of null (aka nothing) to myNumber, which explains the undefined error


#4

Sorry, been a busy month.

The suggestion was valid, not sure if the original code I wrote was that way or not, but adding the additional equal signs allows the script to run but it will still not complete the lesson.

Current code:

var myNumber = 0;
console.log("Initial myNumber = " + myNumber);
if (myNumber == 0 || myNumber == null) {
do {
myNumber = prompt("Please choose a number: ","1-100");
console.log("prompt myNumber = " + myNumber + ", myNumber modulo 2: " + myNumber % 2);
} while (myNumber == 0 || myNumber == null);
console.log("while myNumber = " + myNumber);
};
console.log("myNumber set.");
var isEven = function(number) {
console.log(myNumber % 2 == 0);
if (myNumber % 2 == 0) {
console.log(myNumber % 2 + " Result = True");
return true;
} else if (myNumber % 2 == 1){
console.log(myNumber % 2 + " Result = False; myNumber modulo 3 = " + myNumber % 2 == 1);
return false;
};
};
isEven(myNumber);

When myNumber = 22 console shows:

Initial myNumber = 0
prompt myNumber = 22, myNumber modulo 2: 0
while myNumber = 22
myNumber set.
0 Result = True
true
(503 times)

Error:
Oops, try again. Looks like your function returns false when number = 3. Check whether your code inside the if/else statement correctly returns true if the number it receives is odd.

When myNumber = 23 console shows:

Initial myNumber = 0
prompt myNumber = 23, myNumber modulo 2: 1
while myNumber = 23
myNumber set.
false
false
false
false
false
false
false

Error:
Oops, try again. Looks like your function returns false when number = 2. Check whether your code inside the if/else statement correctly returns true if the number it receives is even.


#5

the exercise runs multiply numbers on your function, but your prompts prevent this from working, in order to pass the exercise you can't have prompts inside your function


#6

Ok, I deleted everything extraneous and hard coded the number:

Current Code:

var myNumber = 22;
var isEven = function(number) {
if (myNumber % 2 == 0) {
return true;
} else {
return false;
};
};
isEven(myNumber);

Same errors using the same (now hardcoded) numbers.


#7

this line:

if (myNumber % 2 == 0)

should use the function parameter (number), what else is the point of giving your function a parameter?


#8

Ugh.. I feel so stupid... I just found it.. I need to do math on number and not myNumber!


#9

Thanks Stetim94!

Working Code:

var myNumber = 0
if (myNumber == 0 || myNumber == null) {
    do { 
        myNumber = prompt("Please choose a number: ","1-100");
    } while (myNumber == 0 || myNumber == null);
}; 
var isEven = function(number) {
    if (number % 2 == 0) {
        return true;
    } else {
        return false;
    };
};
isEven(myNumber);