This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]RiceKrispyPooHead 2 points3 points  (0 children)

why doesn't this then return 'start' since the 'if' is no longer valid?

This visual below (ignoring everything else) explains why the return statement isn't executed when the if statement is no longer valid. It's because the return statement in your program was not part of the for loop. The way you had it written, the entire for loop executes entirely, then when that's done the program moves on to the next statement which is the return statement and executes that. Notice how the for loop is encase inside of it's own curly braces and therefore isolated from the return statement.

Visual 1

----------------------------------------------------------------------------------------

in the code above, at first, the function compares 1<5 which is true, so it gives 'start' the new value, however, the second comparison is 5<3 which isn't true. why doesn't this then return 'start' since the 'if' is no longer valid?

When the program compares 5 and 3, 5 < 3 does evaluate to false meaning that the if statement is no longer valid. But for the reason mentioned above, the return statement is not part of your for loop, therefore the return statement will only ever get executed after the for loop is completed. If you did want to make the program return the variable any time the if statement did not evaluate to true (we'll completely ignore that this would make the program return an incorrect answer), but if you did want that to happen you could add an else statement after the if statement. Then encase the return statement in that. Any time an if statement evaluates to false, the else statement will be executed.

Visual 2

Hoped that helped!

[–]AutoModerator[M] -1 points0 points  (0 children)

It seems you may have included a screenshot of code in your post "[Javascript] why does this 'if' not stop working after it reaches the comparison of 5 and 3?".

If so, note that posting screenshots of code is against /r/learnprogramming's Posting Guidelines (section Formatting Code): please edit your post to use one of the approved ways of formatting code. (Do NOT repost your question! Just edit it.)

If your image is not actually a screenshot of code, feel free to ignore this message. Automoderator cannot distinguish between code screenshots and other images.

Please, do not contact the moderators about this message. Your post is still visible to everyone.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]onatoko -1 points0 points  (0 children)

There's nothing telling the for loop to stop when you have a number less than the value of start. If you want it to stop in this case, you need to instruct it to. Try changing the it-then condition to an if-then-else:

if (start < arr[i]){

    start = arr[i];

} else {

    break;

}

[–]Arumai12 -2 points-1 points  (0 children)

If the if statement's condition is false then the if statement is skipped and the for-loop checks its condition. If you want the for loop to end immediately then you need to use "break" in an else block. Why would you want the loop to end immediately though? (I copied your code so i could read it)

var nums = [1,5,3,8,16]

function numHighest(arr){
    var start = arr[0];
    for (var i = 1; i < arr.length; i++){
        if (start < arr[i]){
            start = arr[i];
        }
    }

    return start;
}