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

0

# 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.

+25

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/

+5

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.

+3

Here i just ran a loop where i checked if a number is even by checking if it had a reminder when divided by 2(if it doesn't have a reminder it's even) and then i displayed that number that didn't have a reminder.The output in this case will be 2,4,6,8,10,12,14.Hope this helped. var nb=15; for(var i=1; i<nb; i++){ if(i%2==0){ console.log(i); } } P.S. 'i' is equal to 1,because if it were equal to 0, then the output would be 0,2,4,6,8,10,12,14, which i think is incorrect.

+2

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

+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

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

for (let i = 0; i < = nb; i+=2){ if (i === 0) continue; console.log (even); }

+1

Explanation: It will take 150/40 = 3.75 hours to cover the distance, which is equivalent to 3.75*60 = 225 minutes. How i solve it

+1

Hey guys please help me #write JavaScript loop that accepts a positive integer n from the keyboard through text box and calculate the sum of square of even digital

+1

The code to display all even numbers in console between 0 and nb should be: //code for (let i = 0; i < nb; i+=2){ console.log(i) } Replacing nb with the desired number or declaring a variable nb which stored the number will make it work.