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**
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Chad Stein
  • 13
  • 3
  • Adding 100 to number within the function does not change the related item in the array. You need to replace it instead. – kaveh Aug 17 '17 at 04:00
  • It would change the value if it was a reference, see https://stackoverflow.com/questions/13266616/primitive-value-vs-reference-value (although that would change your array, so somewhat unrelated to your problem but probably good to know) – ug_ Aug 17 '17 at 04:01

5 Answers5

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;
});
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 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.) – nnnnnn Aug 17 '17 at 04:04
  • Thanks to everyone for your help! I just joined S.O. and your immediate response has been so welcoming. I'm very appreciative. – Chad Stein Aug 17 '17 at 04:13
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])
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
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
Sachin Gupta
  • 7,805
  • 4
  • 30
  • 45
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

techjeffharris
  • 392
  • 2
  • 13
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;
     }
}