Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
ab199
Full Stack JavaScript Techdegree Student 17,446 PointsWhat's wrong with my code? (JavaScript Basics: Stage 4 - Challenge)
aaa
2 Answers
Marcus Parsons
15,719 PointsHiya Yan,
There's nothing wrong with your code, because when I pasted it into a console it works as expected. I have a sneaking suspicion that the problem lies with how you referenced the script in your HTML document. It would help me better to get a snapshot of your workspace so I can see everything you see. If you don't know how to do that, check this post out: http://www.teamtreehouse.com/forum/workspace-snapshots
Matt F.
9,518 PointsI cannot view the code challenge, but is there anything calling the code?
(deleted incorrect information)
Also, just an FYI, you don't need: 'correct < 5 &&' in
else if (correct < 5 && correct >= 3) {
as that part of the conditional will only run if 'correct' is less than 5.
Deleted incorrect information.
Marcus Parsons
15,719 PointsI don't typically downvote answers, but when there is such a maelstrom of misinformation, I have no choice but to downvote your answer.
You do not need a function in order for this code to execute. When the script is referenced in the HTML page (whether that's by using an inline script or by referencing an external script), this code will execute as intended, including the prompts.
Marcus Parsons
15,719 PointsWhy would you put code there to invoke functions that don't exist? That doesn't make sense and is a waste of your time. There's no reason to separate those into separate functions.
Also, when you are talking about self invoking functions, that is not how you do it. You will get an error trying to invoke the function you created. In order to create a self invoking function, you need to wrap the function in parenthesis and then use an outer set of parenthesis to invoke it like so:
(function () {
alert('hi');
})();
Otherwise, you can name the function and then invoke it. Here's one way to do that:
function quiz() {
//quiz code
}
//call the quiz function
quiz();
Matt F.
9,518 PointsHi Marcus,
I did not realize that assigning a prompt to a variable would cause the prompt to execute.
I thought that prompt behaved like other functions. In the case of most functions assigned to variables, the function would not run simply by being assigned to a variable as a function expression.
For example:
var functionExpression = function (){
console.log('hi');
};
will not run until you call that function object stored inside the variable, unless you immediately invoke it.
With that assumption being false, clearly I was incorrect - but if my assumption was correct, it would have been the only way for the prompts to actually run -- and you can downvote my answers any time you want, as I am glad that I learned prompt behaves this way.
Also you are right about immediately-invoked function as well.
Misinformation, however, generally implies an intent to mislead.
Best, Matt
Marcus Parsons
15,719 PointsMatt,
Please don't take it personally but take my comments instead as constructive criticism. I'm not revoking the downvote nor will I apologize for it because it is for a good reason. However, my comment above may have seemed a tad harsh, and for that, I apologize.
With that being said, what you are referring to is a function expression which creates a brand new function. In your case, the new function is of course called functionExpression and you are right in saying that the creation of a new function does not automatically invoke it; it merely creates it. However, take notice of a couple things: 1) You didn't just use the var keyword in the creation of the function. You also used the function keyword. Using those keywords in that order creates a brand new function. So, if I were to put:
var prompt = function () {
alert("this is now my prompt!");
}
now the prompt() function has been overwritten and is an alert box that says "this is now my prompt!" 2) Also take notice that prompt() is a global predefined function and that means it can instantly be invoked. It does not have to be created by anyone because it is created by the browser. That's why you can invoke tons of functions without having to create them.
Matt F.
9,518 PointsThanks Marcus, makes sense.
Marcus Parsons
15,719 PointsNo problem, Matt. Again, I apologize if I seemed rude because that wasn't my intention. I've solved tons of JavaScript problems. If you go to my profile, you can see I'm just a few points shy of 2,000 JavaScript points on the forums alone haha Not bragging, but I'm leading up to saying that if you ever have any questions, just give me a shout: parsons_marcus@yahoo.com. I'm always happy to help out! =]
ab199
Full Stack JavaScript Techdegree Student 17,446 Pointsab199
Full Stack JavaScript Techdegree Student 17,446 PointsHi Marcus, I tested it in Chrome. It was asking questions and then stopped running when the questions were finished. I changed up my code and it works now.
I made a bit of a silly, redundant, mistake. I have set the if statements, to between values, as required by the challenge. I was literally telling JavaScript to run the code that is between two numbers ( 3 and 5). But the more elegant solution is to just use the >= operator. For example:
Could be shortened to this:
The shortened version of this code is more elegant because the other is more redundant. It implies values between 3 and 5 because the first block isn't executed, if it's not equal to 5. It goes to the next section and sees that 4 is a number that is greater than or equal to 3. I hope people reading this can learn from my mistakes.
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsAh, you edited your question after Matt's suggestion of the if-statement being redundant. That's why I ran the code, and it worked exactly as expected. He did get that right out of his response. Most of what he said was incorrect, but the redundancy is correct.
Matt F.
9,518 PointsMatt F.
9,518 PointsHi Yan, just wanted to apologize for getting most of the response wrong. I did not realize that assigning a prompt to a variable would automatically cause the prompt to show up on loading, but I guess it makes sense since the variable cannot be defined without it.
Best, Matt