If/else if/else - invalid left-hand side

javascript

#1

I keep getting the error: " Invalid left-hand argument in assignment " but I'm not sure where the mistake is being made.
Any insight would be appreciated. Thanks!

var isEven = function(number){
if(number%2===0){
return true;
}
else if(isNaN(number) = true){
console.log("That is not a number!");
}
else {
return false;
}
};


#2

@andrewcraven82

Remove the = true because isNaN is a function which determine if the thing is a number. Remember calling a function looks like this:

functionName(optional parameters);

#3

I made that change and you're right I keep forgetting that if/else accepts the condition as being true while evaluating it. It did change the error to "Make sure your if/else if/else statement returns a string when the function input isn't a number" which is bizarre because it does return my string in the console.


#4

@andrewcraven82

You must return a string, not console.log it. Remember return function work like this:

return /* Here your return whatever you want */ ;

#5

Yes. You're correct again. Thank you.

What is the value of returning a string vs. logging it to the console in JS? As you can tell I'm on my first pass through JS.


#6

@andrewcraven82

console.log function is logging something to the console.
return function is returning a value of something.

I don't know if you've ever worked in a real javascript console (other than codeacademy's) in the F12 developer bar, there's a console. If you type a string without using console.log (when we use this function, we use it in a text editor like codeacademy's) like "Hello World", it will return "Hello World" because it recognize what you type is a string so it returns itself.

Here's more information in stack overflow


#7

As said in @miniapple8888's link: They are not even closely linked :slight_smile:

console.log is a function that outputs its parameters to the console and that is all it is doing.

Return is a bit more difficult. To understand return you need to understand functions. Functions are some sort of sub-programs and are not executed immediately like the normal flow of commands (executed from top to bottom). So to make a function run you need to jump inside of it, done by the function call. Then you execute this sub-program and once you're done there you need to return to the main program. And that is done either automatically by the interpreter or manually by using the return statement. So as the function returns automatically if there are no commands left inside of it, the options where YOU want to use return are mainly:

A) if you want to return earlier than expected:
e.g. a wrong user input that makes further things pointless e.g. why bother checking if it is even, if you already know that it is NaN.
B) if you want to send information from the function back to the program.

So the difference is that console.log is a function that outputs to the console whereas return is a keyword that returns with or without value to the program itself.

What happens quite often and which leads to the confusion that @miniapple8888 described is that the codecademy console like many other consoles have the feature that they echo your input value so as he said "Hello World" will show you 'Hello World' in return even without an explicit call of the console.log function. For longer programs this only works for the last entered statement so if the function call is the last statement in your code and therefore the returned value is not used you'll see it in the console, but that does not make return an output to the console it is just a coincidence. if you'd add another value after it, the output would be gone or replaced.

What you could do with your function instead is include it in a bigger program, where you can just use isEven like you did with isNaN:

    yourNumber = parseInt(prompt("Please enter a number"),10);
    
    if(typeof isEven(yourNumber) == "string"){
        console.log("Please enter a number next time");
    }
    else if(isEven(yourNumber)){
        print("Yes "+yourNumber+" is a nice even number");
    }
    else{
        print("That looks odd");
    }

with console.log you'd see true or false on the console but that condition wouldn't work as console.log itself has no value it just prints the value to the console.