#Pushing the right ID Product to an array

91 messages · Page 1 of 1 (latest)

crimson ether
#

Sigh, this is what I get for not checking the work of the people I help.

Question for you: in your handleFoodClick function, what is the menuArray.filter doing? @green geyser

green geyser
#

looking for the right id in the array?

crimson ether
#

Ok, replace your current handleFoodClick function with this:

function handleFoodClick(foodId){ 
    console.log("input ID: ", foodId)
    let targetFoodObj = menuArray.filter(function(food){
        return food.id === foodId
        
    })[0]
    console.log("outputID: ", targetFoodObj)
    
}

(All I've done is add a couple of console logs to it - nothing else has changed)

green geyser
#

yeah but I still get undefined with targetFoodObj

crimson ether
#

Exactly, so what is your filter function doing?

#

(answer: nothing)

green geyser
#

ahh okay, yeah

crimson ether
#

To be frank, there are 2 problems here. First, you started coding without thinking about WHAT you want your program to do. Second, you gave your program a set of instructions that you don't fully understand. Don't worry, these are very common errors and can be fixed

green geyser
#

Yeah I thought I need the ID target so I can get the right product when I click on +. Then I want to push that right product to an array.

#

I got the .filter from my last project and thought I needed that anyways

crimson ether
#

Ok. Let's forget about what functions we're going to use, and think about WHAT we want our program to do. Break down your task into the most basic instructions (imagine you're talking to a very small child).

green geyser
#

yeah ok

#

if right product is pushed, copy paste the content to another array

#

*clicked

crimson ether
#

Not bad. In my opinion there are 4 basic steps to this:

  1. get ID of clicked button
  2. match clicked ID with ID of menu item in menu array
  3. put matched menu item from menu array into order array
  4. display order array on the screen
green geyser
#

I got the first one, right?

#

and even the 2nd one

crimson ether
#

Yes, but we need to think further. Now we know what we want our program to do, we need to think about what information our program needs to do the task. So, what information does our program need?

green geyser
#

it wants to know which food it needs to push where

crimson ether
#

How does the program know which food item to put into the order?

green geyser
#

I am really confused how to push from 1 array to another :/

#

I only know how to hardcode the push to an array

crimson ether
#

You're still thinking in syntax - I think you to think in English. Forget arrays and functions and all of that. We're planning here. (We'll get to how to push stuff into arrays and do all that later, I promise).

green geyser
#

Ok

crimson ether
#

Imagine this: I come up to you and say "put that food item on my order" - what would you say in reply?

green geyser
#

sure

#

and click + 1

crimson ether
#

"That's the wrong item"

green geyser
#

which one?

#

I would ask

crimson ether
#

Right. So I reply "number 1"

green geyser
#

I say no problem and press number 1

crimson ether
#

But what does number 1 mean?

green geyser
#

it means Hamburger in my array

#

which cost 12 and is beef, cheese and lettuce

crimson ether
#

Yes, you know that, but does the program know that?

green geyser
#

no

crimson ether
#

So, what information does your program need to do the task?

green geyser
#

It needs to know which item is what?

crimson ether
#

Good, what else?

green geyser
#

it needs to know which item was clicked

#

and where to put it

crimson ether
#

Good, so in total we need these pieces of information:

  1. the selected menu item
  2. all possible menu items
  3. where to put the selected menu items
green geyser
#

yes

crimson ether
#

It may seem silly but that's how we need to think when we're programming - What do we want our program to do, and what information does our program need. If we don't know that, we have no chance

green geyser
#

yeah it makes sense

crimson ether
#

Now, look at your code, do you have all the pieces of information you need?

green geyser
#

no

#

I still don't know how to get them from the array and paste them to another one

crimson ether
#

You do, you're just not understanding the instructions you're giving your program

green geyser
#

yes

#

I know I can write orderArray.push[]

#

Something like this? orderArray.push[menuArray]

crimson ether
#

You can, but before that we need to get the correct menu item that the user selects

#

Replace your current handleFoodClick function with this:

#
function handleFoodClick(foodId){ 
    console.log("input ID: ", foodId, typeof foodId)
    let targetFoodObj = menuArray.filter(function(food){
        console.log("food.id: ", food.id, typeof food.id)
        return food.id === foodId
        
    })[0]
    console.log("outputID: ", targetFoodObj, typeof targetFoodObj)
    
}
#

Just as before, the logic is the same, I've just added more console logs

green geyser
#

ok yes

crimson ether
#

Do you see the problem?

green geyser
#

the input is a string?

crimson ether
#

Our input ID is a string, but food.id is a number

#

question for you: What's the difference between == and === ?

green geyser
#

no Idea, === is absolut and = not or something

#

but == no idea

#

ahh

#

I have == now

crimson ether
#

Ok, both of them are comparison operators (i.e. compare this to that and tell me if they are equal). The difference is that == only checks the values, whereas === checks the types as well as the values.

green geyser
#

I got now the WHOLE name of the thing :DDD

#

So I need ==

#

and now push targetFoodObj to the empty ARRAY

crimson ether
#

Yep. So, for troubleshooting you need to check not only the value of things, but the type of them too. typeof is very useful for that

green geyser
#

ahh ok

crimson ether
#

Whenever you're doing any kind of comparison, always double check the variable types

green geyser
#

yes I will do that

#

orderArray.push(targetFoodObj) sends the Product to the array, but I need only name and price

crimson ether
#

For now I would just leave that as it is. Better to have too much information than too little

green geyser
#

But the name?

crimson ether
#

What about the name?

green geyser
#

I need that to be pushed aswell

#

orderArray.push(targetFoodObj["name", "price"])

#

like this?

crimson ether
#

Is it not inside the array already?

green geyser
#

oh you want me to push the whole thing?

#

orderArray.push(targetFoodObj)

crimson ether
#

yes

green geyser
#

ok

#

now I would build a template string where I would show the clicked item

#

I would make an IF (orderArray > 0) ?

#

wait no, IF (orderArray.length > 0)

green geyser
#

I am off for today, thanks for all your help