I need help with javascript | Sololearn: Learn to code for FREE!

+1

I need help with javascript

Hey guys please help me #Write a loop in JavaScript that will display all even numbers in the console between 0 and "nb". In the example below, the variable "nb" is equal to 15.

4/10/2021 4:09:22 PM

ABDELJALIL ESSOUALHI

24 Answers

New Answer

+17

ABDELJALIL ESSOUALHI Have you tried this yourself? If you have, please show us your attempt. You are more likely to get help when you post your attempt and ask a specific question, rather than something like "please help me do this whole task".

+10

for (var x=0; x<=15; x++) { if (x === 0) { console.log(x + " is even"); } else if (x % 2 === 0) { console.log(x + " is even"); } else { console.log(x + " is odd"); } }

+8

If all you need to do is display even numbers and you don't care about odd ones, you can do this: for (let x=0; x<15; x+=2) { console.log(x + " is even"); } Just replace 15 with your variable nb to make it work for any value.

+6

https://www.sololearn.com/learning/1140/

+4

Is this work too ? var nb = 15; for (var x=2; x<=nb; x+=2) { console.log(x); }

+4

ABDELJALIL ESSOUALHI that will ignore 0. So if you want to get 0 as an even number too, change var x = 2 to var x = 0. Also, x <= nb will include the nb value as well if it's even. For example, if nb was 16 you would get 16 printed out too. If you want only numbers up to nb but exclude nb, change to x < nb.

+2

For( let x=0; x<15; x+=2){

+2

I need a javascript expert how can I contact you

+2

/* try this code; this is important for beginners to understand */ let n = prompt ("Find even numbers upto what number", 14); let x; for(x = 0; x <= n; x++){ if ( x % 2 === 0 && x != 0 ){ console.log(x); } }

+2

/* this should work. but not tested. */ var nb = prompt("even nums from 0 to your answer:"); for(var a = 0; a <= nb; a += 2){ console.log(a); }

+1

I need a javascript expert how can I contact you

+1

+1

let x = 2; for( x=2; x<=15; x+=2){console.log(x);} This will work

+1

for (var x=0; x<=15; x++) { if (x % 2 === 0) { console.log(x + " is even"); } else { continue; } }

+1

function number(nb) { console.log('even numbers are') for(let i = 0; i<=nb; i+=2) { console.log(i); } } const myInput = 15; const numbers = number(myInput);

+1

let nb = 15; let num = 0; for(;num <= 15;){ if(num % 2 != 0 && num != 0){ console.log(num + " is odd"); num++; }else{ console.log(num + " is even"); num++; } }

0

which the ploblem?

0

I'm not sure

0

JavaScript developer is easy but being a JavaScript master is a little difficult. Just going through the concepts will not do enough. It is the should to HTML which gives HTML some life and you see beautiful elements performing tasks they never imagined.

0

let nb = 15 for (let n = 0; n < nb; n++) { if (n > 0 && n % 2 === 0) console.log(n) }