forEach is/isn't setting

the console log 1 shows as expected (2D array, second element inner arrays numbers), and the console.log 2 displays as expected with iterations finally setting all numbers to 0.
then console log 3 shows undefined.
i’m tiring and cant work out why?


var change=cid

  console.log('1:',change)

  change=change.forEach(function(elem){

    elem[1]=0

    console.log('2:',change)

  })

  console.log('3:',change)

That really doesn’t look like an intended way to use a forEach. What end result are you trying to get?

i was just trying to set all the numbers to 0
(2nd element of each inner array)
(i could do it with a for loop but i thought this was supposed to be basically the same)

i actually just wanted to do this:

change=cid.forEach(elem=>elem[1]=0)

Why not use a map?

i’ll have a look now.
but i will still be left wondering about my forEach mind anomoly

sorry, do you mean .map or build an array map of variables?

You provided very little code, so it is hard to understand what you are trying to do.

In general, changing an object or arrays you iterate over it with array methods. For every correct way to do this, there are dozens of wrong ways to do this.

So what are you trying to do? What do you start with and what do you want to end up with?

its the cash register one

i’m building the ‘change’ array from the ‘cid’ array but need to set the values to zero. just to have the denominations in a 2D array with 0 quantity for each.

function checkCashRegister(price, cash, cid) {

  //var change=[["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]

  var change=cid

  console.log('1:',change)

  change=cid.map(function(elem){

    elem[1]=0

    console.log('2:',change)

  })

  console.log('3:',change)

  ....

  
  change=( change.filter((elem)=>elem[1]>0) ).reverse()

  ....

  return {status: "OPEN", change: change}

}

console.log(

checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])

)

(i already have a working function but i’m trying to change things and more concerned about learning rather than completing the exercise)

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Right, so making a new array from an old array is exactly the place where you should use a .map() instead of a .forEach().

There is a few reasons for this

  1. As a general rule, you don’t want to change the input variables unless your function description explicitly says that it does this.

  2. You are trying to capture the return value, but for a .forEach() the return value is undefined.

  1. .map() is the method for creating a new array from an old array.

Side Note: You should never use var. You should use const for almost everything. You should use let for primitive variables you want to change.

1 Like

so it seems to work as:

  let change=cid.map(elem=>[elem[0],0])

i wasn’t expressing the function properly as well as using wrong one!
lol

thanks

can you confirm ‘let’ is right choice or ‘const’?

1 Like

use always const, if you get an error because you are trying to change something declared with const, then change it to let

1 Like

It’s fairly unlikely you will want to reassign the value of a variable that contains an array or object. More likely you will use some non-mutating method on it and return a new copy. Reassignment should really be kept to a minimum and if you do need such a variable it should be pretty obvious.

1 Like

this variable certainly changed in my code. are you guys saying good practice is to use more variables in order to leave things unmutated?
i think i have to leave change as a changable ‘let’ in cash register( pun unintended). but i’d like to know the etiquette…we are supposed to use const at the expense of extra variables? i have been using ‘let’ a lot and updating variables/assignments.

Did the variable change which array it was pointed at, or did the contents of the array change?

const someArray = [0, 1, 2];
someArray[1] = 42; // This is fine

no the array changed as it is used as a return array so is the central point of the function

no…wait…

you’re right, but all the elements changed in 2 loops/function calls, and it was reversed. so i guess it’s as you suggested…it was just elemental changes?
(i’m trying not to post project code on forum, i think you guessed right tho)

My rule of thumb is to always use const and then if you decide I need to change the primitive value stored in the variable, then swap to let.

Arrays can be a bit tricky if you aren’t familiar with pointers. Basically, a JavaScript variable can only hold one thing. There is more than one piece of information needed to make an array or object, so a variable that holds an array or object actually is telling your browser where in your computer’s memory the full array or object has been stored.

So, you should use const for arrays and objects unless you what to change which array/object your variable is talking about.

let change=arrayA.map(elem=>[elem[0],0])
change[i][1]=change[i][1]+arrayB[i]
change=( change.filter((elem)=>elem[1]>0) ).reverse()

are these changes reasonable? or shud have another variable name assigned?

This works fine with const.

This is making a new array, so I would call it something new. This should work fine without the extra set of ()s, by the way.

1 Like

sorted, thanks people.
(might be about time for another paypal to FCC tomorrow)