1

*Use the existing test variable and write a forEach loop * that adds 100 to each number that is divisible by 3. * * Things to note: * - you must use an if statement to verify code is divisible by 3

I'm confused, why isn't my code working?

var test = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4,
19, 300, 3775, 299, 36, 209, 148, 169, 299,
6, 109, 20, 58, 139, 59, 3, 1, 139
];

test.forEach(function(number) {
if (number % 3 === 0) {
    number += 100;

});


console.log(test[0]); **this is returning 12, NOT the desired 112**
2
  • Adding 100 to number within the function does not change the related item in the array. You need to replace it instead. Commented Aug 17, 2017 at 4:00
  • It would change the value if it was a reference, see stackoverflow.com/questions/13266616/… (although that would change your array, so somewhat unrelated to your problem but probably good to know) Commented Aug 17, 2017 at 4:01

5 Answers 5

4

You are not putting back the number in array.

Primitives are not references. You need to use the index and put it back.

test.forEach(function(number,index) {
if (number % 3 === 0) {
    number += 100;
    test[index] = number;
});
Sign up to request clarification or add additional context in comments.

2 Comments

Or if you wanted to make the callback more generic you could use all three callback arguments, function(number, index, array) and then array[index] = .... (So that the callback doesn't need to know about the test variable.)
Thanks to everyone for your help! I just joined S.O. and your immediate response has been so welcoming. I'm very appreciative.
1

You can write a function that doesn't need to access its scope like some of the other answers here do, using forEach()'s third argument:

arr.forEach(function callback(currentValue, index, array) { ...

let test = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4,
  19, 300, 3775, 299, 36, 209, 148, 169, 299,
  6, 109, 20, 58, 139, 59, 3, 1, 139
]

test.forEach(function (number, index, array) {
  if (number % 3 === 0) {
    array[index] = number + 100
  }
})

console.log(test[0])

Comments

0

You need to include index in your for-each loop. and use that index to change value in actual array.

working code:

var test = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4,
19, 300, 3775, 299, 36, 209, 148, 169, 299,
6, 109, 20, 58, 139, 59, 3, 1, 139
];

test.forEach(function(number, i) {
if (number % 3 === 0) {
    test[i] += 100;

}
});


console.log(test[0]); //print 112

Comments

0

As others have said, you need to set the index from which the number was read to the value+100

Javascript has a lot of not-so-intuitive quirks, and function arguments spare no expense. Check out this article for a bit more detail about how Javascript passes values/references to functions: https://stackoverflow.com/a/6605700/1690165

Comments

0

you can use forloop/foreach:

var test = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4, 
            19, 300, 3775, 299, 36, 209, 148, 169, 299, 
             6, 109, 20, 58, 139, 59, 3, 1, 139 ];

foreach

foreach (int i in test)
{
    if (i % 3 === 0) {
         i += 100;
     }
}

forloop

for (i = 0; i < test.Count; i++)
{
     if (test[i] % 3 === 0) {
         test[i] += 100;
     }
}

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.