we have an array of numbers and we only want to print out the even ones: (part of code is missing in the note i have and i cant figure it out!)
let numbers = [1, 2, 3, 4, 5, 6]
var i = 0;
while (i
if (numbers[i] % 2 != 0) { continue; } print(numbers[i]);}
Top comments (8)
Probably something like this would do the job.
You could also do the same thing using a for-loop
Something i don't understand is that, in your post title, you've mentioned that you're using javascript, but, to display something in the console using javascript, you should use
console.log
notprint
because theprint
command displays the pop window to print something with your printer.I don't use continue often but doesn't it break the loop iteration? Meaning that would lead to an infinite loop in the while example because i would not get incremented.
Yeah, i forgot that point, if you're using a while loop, you should increment
i
before the if statement. otherwise, it will create an infinite loop. If you're using a for loop, the code i wrote above is OKyes i use 2 online compiler to learn basics right now jsbin and other one, one of them needs print to print out the result, i went to check on both sites before posting here thats why this copy paste i did here was with print command, thanks for solving this code for me, hit a like so bad on that, little bit stupid i think of me
ah thank you man! appreciate it so much!
ok with all your inputs i think i found the right solution for this, in my original notes it was asking with while loops and continue statement get the all the even numbers within an array:
There can be many solutions for it .One of the solutions can be
i posted this thinking problem is so basic that probably no one would even reply, feels good :)