Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  69] [ 7]  / answers: 1 / hits: 9686  / 10 Years ago, fri, may 23, 2014, 12:00:00

I'm just trying to figure out why this code isn't working.



var programming = false;
var happy = function() {
if(programming === true) {
happy = false;
}
else {
happy = true
}
};


or my secondary code



var programming = false;
var happy = function() {
if(programming === true) {
happy = false;
}
if(programming) {
happy = true;
}
};

More From » function

 Answers
2

I think you mean



var programming = false;
var happy = function() {
if(programming === true) {
return false;
}
else {
return true;
}
};


This is how javascript works. You set the return value of a function using the keyword return, not by reassigning the function to it's return value.



What happens with your code is that the first time the function is called, it replaces itself by it's return value (that is a boolean). The second time you try to call it, the function doesn't exist anymore, because the variable happy now contains a boolean (the result of the first time you called it).


[#45080] Thursday, May 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kalistaz

Total Points: 0
Total Questions: 100
Total Answers: 106

Location: Bermuda
Member since Thu, Apr 20, 2023
1 Year ago
;