-1
var brackets = [];
for(i = 0; i < 5; i++){
    brackets.push(15 += 5)
}

I want to use this code to add a new element to the array each time, but the new elements have to be added in increments of 5, starting from 15. It will go 15, 20, 25, 30, 35, 40.

4
  • 2
    What do you expect 15 += 5 to do? Commented Jul 17, 2014 at 22:13
  • I've tried this: var brackets = [15]; for(i = 0; i < 5; i++){ brackets.push(brackets[0] += 5) } But it gives this output: [ 40, 20, 25, 30, 35, 40 ] Commented Jul 17, 2014 at 22:14
  • I just want to start from 15 and add 5 each time for every new element Commented Jul 17, 2014 at 22:15
  • 1
    Then do just that: for (var i = 15; i <= 40; i += 5) { ... } Commented Jul 17, 2014 at 22:28

3 Answers 3

5
var brackets = [];
for(i = 0; i <= 5; i++){
    brackets.push(15+5*i))
}
Sign up to request clarification or add additional context in comments.

Comments

1
var brackets = [];
for(var i = 15; i < 45; i+=5){
    brackets.push(i);
}

Comments

0
var brackets = [];
for(i = 0; i < 5; i++){
    brackets.push(15 += 5)
}

First of all, when you put "15 += 5" means nothing, because 15 is a number not a variable.. unless you put a variable "a" like this:

var brackets = [15];
var a=15;
for(i = 0; i < 5; i++){
    brackets.push(a += 5)
}

and

var brackets = [15];
for(i = 0; i < 5; i++){ brackets.push(brackets[0] += 5) } 

But it gives this output: [ 40, 20, 25, 30, 35, 40 ]

well.. "brackets[0]" is a variable... when you do "brackets[0] += 5" it will do 15+5 and store the result (20) on brackets[0], after that it does "brackets.push(20)" in which brackets has now [20,20].. the second time it will be [25,20,25], after that [30,20,25,30] and so on until you get [40,20,25,30,35,40].. A solution for this one would be:

var brackets = [15];
for(i = 0; i < 5; i++){ brackets.push(brackets[i] + 5) } 

Comments

Your Answer

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.