1. If/Else -- Hyperactive console readout?!


#1



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


Hello again! Bet you missed me since I last asked for help... Now I have a very curious issue. The code below makes sense to me and is actually approved of by the system. However... the console-readout for this script is extremely hyperactive. It prints out

You chose the number 12 .
Your number was not divisible by 2.

Followed by way more than 100 repeats of

Your number was divisible by 2.

Which after some number (exceptionally large number....) of repeats then outputs a (guessingly) equal and large number of repeats of

Your number was not divisible by 2.

So, I like to figure things out myself the best I can, so I tried to look online for console output line repeats, etc. to figure out whether anyone else has seen this occur and why, but couldn't find anything relevant (:frowning2:). So I'm now looking to your veritable expertise to help me understand why this apparently simple code is resolving so strangely?


var userNumber = prompt("Please enter an integer.");
console.log("You chose the number " + userNumber + " .");

function isEven (userNumber) {
    if ((userNumber % 2) === 0) {
        console.log("Your number was divisible by 2.");
        return true;
    }
    else {
        console.log("Your number was not divisible by 2.");
        return false;
    }
}


#2

Hello @silasmao,

In that exercise, you do not have to analyse user input. All you need to do is make the function isEven, and you'll be good to go!


#3

Hello @bartholomewallen

Unfortunately that was all of it, and it still put output. And then when I added calling the function later to see if that changed anything, I then got the number readout, statement it was divisible by 2, statement it wasn't, then tons of statements that it was, tons of statements it wasn't, and then "true".

I'll go ahead and change it so that it is a simpler answer to the question that was given to me. I just wish I understood what was happening. I think the problem might have something to do with interaction between the console.log action and the return action.

Maybe I'll come back to this when I have more experience and understanding of Javascript? I'll mark this thread as answered, since in general I can complete this step.

Thanks!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.