0

I'm trying to learn more about arrays and so I just wrote this simple function:
(I'm calling doThis from an onClick for testing purposes).

function doThis() {
    var multiplier = 3;
    var list = [23, 54, 23, 25, 65];
    var products = multiply(list, multiplier);
    console.log(products);
}
function multiply(list, multiplier) {
    list.forEach(function (element){
        var multi = [element * multiplier];
        console.log(multi);
        return multi;
    });
}

My issue is two-fold:

  1. var products is returning undefined.
  2. When I step through the code in the debugger, it shows the arithmetic being done correctly, but it doesn't store more than one value in the variable multi.

The end goal would be to have var products hold the multi array.

1
  • You should push multiplied item to some array, which you can return after looping Commented Jul 5, 2017 at 18:35

5 Answers 5

1

Use map() When you are planning to return a new array from an existing array. forEach() let you iterate the array but doesn't return any new array.

function doThis() {
    var multiplier = 3;
    var list = [23, 54, 23, 25, 65];
    var products = multiply(list, multiplier);
    console.log(products);
}
function multiply(list, multiplier) {
   return list.map(function (element){
        return element * multiplier;
    });
}

doThis();

Sign up to request clarification or add additional context in comments.

Comments

0
 function doThis() {
    var multiplier = 3
    var list = [23, 54, 23, 25, 65]
    var products = multiply(list, multiplier)
    console.log(products)
}

function multiply(list, multiplier) {
var multi = [] ;
list.forEach(
   function (element){
       multi.push(element * multiplier)
       console.log(multi)
       //return multi
   }
);
return multi ;
}

1 Comment

0

Using return in a forEach function on an array will not change the values of the original array.

You need to specify where you want to set a value. I suggest using the second parameter in the forEach anonymous function, being the index of the element.

Then you can easily do list[index] = "my new value";

Also, putting the value of the multiplying action between brackets (you did var multi = [element * multiplier];) creates a single-element array for each value.

Here's how I did it to keep a one dimension array:

function doThis() {
    var multiplier = 3
    var list = [23, 54, 23, 25, 65]
    var products = multiply(list, multiplier)
    console.log(products)
}

function multiply(list, multiplier) {
    list.forEach(function(element, index) {
        list[index] = element * multiplier
    });

    return list;
}

doThis();

Comments

0

You could use a for loop while you're learning about arrays:

function multiply(list, multiplier) {
    var multi = [];
    for (i = 0; i < list.length; i++) {
        multi.push(list[i] * multiplier)
    }

    return multi;
}

Comments

0

There are multiple points here:

  1. the function multiply() is not returning the return from list.forEach(). Add the return keyword before list.forEach(function (element){
  2. forEach returns undefined.

Return value

undefined
1

To return the result from each iteration, use map() or manually add each return value to the array.

See the changes in the snippet below.

To learn more about map() and similar functions, try these exercises.

function doThis() {
  var multiplier = 3
  var list = [23, 54, 23, 25, 65]
  var products = multiply(list, multiplier)
  console.log('products: ', products)
}

function multiply(list, multiplier) {
  return list.map(
    function(element) {
      var multi = [element * multiplier]
      return multi
    }
  );
}
document.getElementById('doThis').addEventListener('click', doThis);
<button id="doThis">doThis()</button>


1https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Return_value

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.