1

I'm trying to make this loop work in order to get the multiples of 5 that are below 1000 in an array (yeah, just getting started with euler), but it keeps crashing my console:

var multiploCincoArray = []; 
for(i = 1, r = i * 5; r < 1000; i++) {
 multiploCincoArray.push(r);
}
console.log(multiploCincoArray);

I know there's something wrong with the stopping condition but i can't seem to find it.

I know i can do this too:

var multiploCincoArray = []; 
for(i = 1; i <= 199 ; i++) { 
 multiploCincoArray.push(5 * i);
}
console.log(multiploCincoArray);

but i want to follow the path shown in the first script (if possible)...

jo_va
14.1k3 gold badges25 silver badges49 bronze badges
asked Feb 13, 2019 at 20:32

5 Answers 5

2

You could move the calculation into the conditional part.

var multiploCincoArray = [];
for (var i = 1, r; r = i * 5, r < 1000; i++) {
 multiploCincoArray.push(r);
}
console.log(multiploCincoArray);

answered Feb 13, 2019 at 20:34
Sign up to request clarification or add additional context in comments.

Comments

2

Try that :

var multiploCincoArray = []; 
for(i = 5; i <= 1000; i = i + 5) { 
 multiploCincoArray.push(i);
}
console.log(multiploCincoArray);
answered Feb 13, 2019 at 20:38

Comments

1

Your approach doesn't seem right imo.

Firstly start with a single variable i, and iterate till 1000.

for (var i=1; i<1000; i++)

check if the i is a multiple of 5 via i%5, if it is then push the value in an array.

var array = [];
for (var i=1; i<1000; i++) {
 if (i%5 === 0) {
 array.push(i);
 }
}
console.log(array);

answered Feb 13, 2019 at 20:36

2 Comments

This will be five times slower than the approach the OP is trying (if it would be fixed)
Just showing how to approach the question, you don't even need the loop to test values, you can simply start with the min possible multiplier and keep adding the multiplier. @JonasWilms
1
 for(i = 1, r = i * 5; r < 1000; i++){

Is the same as:

 i = 1, r = i * 5
 while(r < 1000) i++;

So actually you set r just once (to 5), then you increase i as long as r is smaller than 1000, but as r doesn't change the loop is infinite and crashes your browser.

You might just do:

 for(let i = 5; i < 1000; i += 5)
Luca Kiebel
10.1k7 gold badges34 silver badges47 bronze badges
answered Feb 13, 2019 at 20:35

1 Comment

thanks a lot, i couldn't see why r wasn't changing!.
0

On the right track but condition in the wrong place. Change for(i = 1, r = i * 5; r < 1000; i++) to for(i = 1, r=5; r < 1000; r=i*5,i++)

Should use let i and let r for scope but I left it out for clarity.

answered Feb 13, 2019 at 21:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.