Print every second number?

0 Marin Bešlo · August 16, 2015

I should take two numbers and then print out every second number between those 2 numbers,
but they must be divisible with 2 and 3. I did it like this...

var jedan = parseInt(prompt("First number:"));
var dva = parseInt(prompt("Second number:"));
var aaaa= [];

for(var i = jedan; i <= dva; i++){
if(i%2===0 && i%3===0)
var i = 1;
while(i < aaaa.length){
document.write(aaaa + " ");

And it works, but I first tried with a foor loop instead of this while loop (for (var i = 1; i < aaaa.length; i+2)),
and it didn't work. Does anyone know why? 

Post a Reply


Oldest  Newest  Rating
0 wernich wilsenach · August 16, 2015
first thing i see is you're writing the whole array every time the while loop runs. you won't notice it because you're overwriting the webpage each time, so it only appears once. if you change the "document.write" to "console.log" then you'll see in the console that it's written each time.

i got it to work fine with a while and for loop (copied your code and made only the change to "console.log"). what error do you get in the console?
0 Marin Bešlo · August 16, 2015
Actually, I was working in Notepad++, that's why I used document.write :D
and now I fixed it, i forgot to put
document.write(aaaa + " ");
in my while loop :D

Thanks ;)
  • 1



A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.