-2

Javascript is something new for me, and we have to do homework.

I have created new array:

var numbers = [1,2,3,4,5,6];

And with function forEeach I should achieve result like in console.log:

console.log(numbers[0]*numbers[1]+numbers[0]+numbers[1]);

I've tested many things, but I don't have any idea how to pull out signle init...

I know it should be simple, but I've stucked. Thanks for help!

Dario
  • 6,152
  • 9
  • 39
  • 50
Cinnamon
  • 7
  • 2
  • 3
    please add the wanted result and the `forEach` part. – Nina Scholz Dec 23 '17 at 18:00
  • you can create new array of elements using your formula and `map` function... and then just sum all that items like here https://stackoverflow.com/a/16751601/3917754 – demo Dec 23 '17 at 18:04
  • Since this is stated as homework, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach – Mark Schultheiss Dec 23 '17 at 18:17
  • 2
    Is that first in your console really a multiply `*` there or a typo? Please clarify based on your title the exact intent here i.e. your exact problem description is not 100% clear here. – Mark Schultheiss Dec 23 '17 at 18:23

3 Answers3

0

From your question looks like your problem is interacting with the current element of the forEach loop.

var numbers = [1,2,3,4,5,6]

// this will print every number in the array
// note that index numbers are not needed to get elements from the array
numbers.forEach(function(num){
  console.log(num)
})

Now, if what you're trying t achieve is sum and multiply every int (as stated in the question title), you could do it like this

var numbers = [1,2,3,4,5,6]
var sumResult = 0
var multiplicationResult = 1

// the function will be evaluated for every element of the array
numbers.forEach(function(num){
  sumResult += num
  multiplicationResult *= num
})

console.log('Sum', sumResult)
console.log('Multiplication', multiplicationResult)

However, a more appropiate approach could be obtained by using reduce like this:

var numbers = [1,2,3,4,5,6]

var sumResult = numbers.reduce(function(result, num){
  return num+result
}, 0)

var multiplicationResult = numbers.reduce(function(result, num){
  return num*result
}, 1)

console.log('Sum', sumResult)
console.log('Multiplication', multiplicationResult)

Hope this helps.

More info:

Ramses
  • 996
  • 4
  • 12
  • 28
  • 1
    Why not `let { sum, product } = numbers.reduce((o, n) => (o.sum += n, o.product *= n, o), { sum: 0, product: 1 })`? – Patrick Roberts Dec 23 '17 at 18:50
  • Totally valid! however, as the asker states that is new to JS, did not want to add complexity to the answer by adding objects or ES6 syntax – Ramses Dec 23 '17 at 20:21
0

To pull out a single number for the provided array you using the indexer/bracket notation which is specifying a number (length of array - 1) in brackets, like below:

var numbers = [1, 2, 3, 4, 5, 6];
numbers[0]; // selects the first number in the array
numbers[1]; // selects second number etc.

To sum up the numbers using forEach, simply do:

var sum = 0;

numbers.forEach(function(number) {
  sum += number; // add number to sum
});

forEach goes through all the numbers in the numbers array, passing in each number to the function defined and then adds the number to the sum variable.

codejockie
  • 9,020
  • 4
  • 40
  • 46
0

If you want your results, use map(). Unlike forEach(), map() will always return results in a new array. It wasn't very clear as to what expression you are expected to use or what the result of said expression should be so this demo will do the following on each iteration:

  • A = current value * next value
  • B = current value + next value
  • C = A + B;

Demo

const num = [1, 2, 3, 4, 5, 6];

let arr = num.map(function(n, idx, num) {
  let next = num[idx + 1];
  if (!next > 0) {
    next = idx + 2;
  }
  let subSUM = n + next;
  let subPRD = n * next;
  let subRES = subPRD + subSUM;
  return subRES;

});

console.log(arr);
Community
  • 1
  • 1
zer00ne
  • 41,936
  • 6
  • 41
  • 68