Quiz: Um outro tipo de loop (6-8)
Instruções:
Use o método
forEach() para fazer o loop sobre a seguinte array e adicione 100 para cada um dos valores se o valor for divisível por 3. Imprima o array test no console.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];
Código
tip1
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]; // Write your code here var element = ""; test.forEach(function(element, index, array) { if (element % 3 === 0) { array[index] = 100 + element; } }); console.log(test);
tip2
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];
// Write your code here
test.forEach(function(currentValue, index, array) {
if (currentValue % 3 === 0)
array[index] += 100;
});
console.log(test);
Comentários
Postar um comentário