简体   繁体   中英

cart challenge javascript logic. is there more than one way to solve?

I just finished the JS intro with code academy, so im kinda new to this. Please look at the following 2 pieces of code, of a Shopping Cart Challenge. The first one is the correct code that I found online, in which It scans an array that is already defined and then when inputting a piece of information (in this case "item") an output of another piece of information ("price") is received.

Here is the "right" solution:

 // Declare Array var shoppingCart = []; // Declare function addToCart addToCart = function(name,price) { this.name = name; this.price = price; shoppingCart.push(this.name,this.price); }; function priceCheck(itemName) { var i = 0; for(i; i <= shoppingCart.length; i++) { if(itemName === shoppingCart[i]) { console.log(shoppingCart[i += 1]); break; } else {console.log("the searched item is not in the shopping cart");} } } 

as you can see, this person used .this.

In my code i just added an object (like was instructed). Is my logic completely off, or i just need to do some repairs?

My code:

 shoppingCart = []; function addToCart(itemName, itemPrice){ const shiny = { name: itemName, price: itemPrice }; shoppingCart.push(shiny); console.log(shoppingCart); } function priceCheck(itemName){ for(i=0;i<=shoppingCart.length; i++){ if(itemName===shoppingCart[i]){ return itemPrice; } else{ console.log('item isnt in cart'); }}} addToCart('apple',20); console.log(shoppingCart); priceCheck('apple'); 

It works fine until it reaches the priceCheck('apple'); - then it seemes i cannot find 'apple' in the array.

I try to understand if I need to use the (.this) and if I can do it the way I wrote.

PS - sorry if its a mess, its a bit hard for me to explain what im trying to understand :) PSS - Im not just looking to solve the challenge but really develop a "coding mindset for problems".

THX!

The "right" solution? Rather not.

 addToCart = function(name,price) {

This is an undeclared and unneccessary function expression. One should rather do:

 function addOne(name, price){

And accessing this here is not really a good practice cause its missleading. I would always only use this if you are just refering to OOP code. Additionally the following makes no sense to me:

 shoppingCart.push(this.name,this.price);

cause that adds the name and the price as different array elements. And this sets variables that are never used (why??):

 this.name = name;
 this.price = price;

So your solution is actually better than the "right" one IMO. However there are a few things:

for(i=0;i<=shoppingCart.length; i++){

This iterates until i is equal to the length, but as arrays are zero based there is no element at that position. Do:

 for(let i = 0; i < shoppingCart.length; i++)

Additionally, objects are compared by reference, but you actually want to compare them by value. Eg you could take the name property:

  if(name === shoppingCart[i].name)

And a small style tip, dont do this:

}}}

How i would do that:

  const cart = [];

  function addToCart(name, price){
    cart.push({name, price});
  }

  function priceOf(findName){
    for(const {name, price} of cart)
       if(findName === name) return price;
    return NaN;
  }

An obvious issue is here:

if(itemName===shoppingCart[i]){

You're comparing itemName to the object containing name and price instead of the name.

I don't remember the exact JS syntax for this but it should be something like

if (itemName === shoppingCart[i].name) { 

shoppingCart[i] is an object and name and price are its properties.

You need to access the properties of the object instead.

replace itemName == shoppingCart[i] with itemName == shoppingCart[i].name and then you can get the price with shoppingCart[i].price

 shoppingCart = []; function addToCart(itemName, itemPrice){ const shiny = { name: itemName, price: itemPrice }; shoppingCart.push(shiny); console.log(shoppingCart); } function priceCheck(itemName){ for(i=0;i<=shoppingCart.length; i++){ if(itemName===shoppingCart[i].name){//here return shoppingCart[i].price;//here } else{ console.log('item isnt in cart'); }}} addToCart('apple',20); console.log(shoppingCart); console.log(priceCheck('apple')); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM