DEV Community

laryken
laryken

Posted on

What am i missing please. it doesn't give me the total. it only evaluates the first if

`function inventoryScore (inventory) {
const splited = inventory.split(',')
let totalInventory = 0
for (let i = 0; i < splited.length; i++) {
const item = splited[i]
if (item === 'gold cup') {
totalInventory += 5
} else if (item === 'puppy') {
totalInventory += 4
} else if (item === 'magic cup') {
totalInventory += 10
} else if (item === 'tooth of a magestic whale') {
totalInventory += 20
} else if (item === 'tentacle of a giant squid') {
totalInventory += 100
} else if (item === 'anything else') {
totalInventory += 1
}
}
return totalInventory;
}

console.log(inventoryScore('gold cup, puppy, magic cup, tooth of a magestic whale, tentacle of a giant squid, anything else'))

// let str = 'gold cup, puppy, magic cup, tooth of a magestic whale, tentacle of a giant squid, anything else'`

Top comments (3)

Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

Hi Laryken, first of all you can formate the code using markdown so we can interpret it better. This time I'm doing it for you:

function inventoryScore(inventory) {
  const splited = inventory.split(',');
  let totalInventory = 0;
  for (let i = 0; i < splited.length; i++) {
    const item = splited[i];
    if (item === 'gold cup') {
      totalInventory += 5;
    } else if (item === 'puppy') {
      totalInventory += 4;
    } else if (item === 'magic cup') {
      totalInventory += 10;
    } else if (item === 'tooth of a magestic whale') {
      totalInventory += 20;
    } else if (item === 'tentacle of a giant squid') {
      totalInventory += 100;
    } else if (item === 'anything else') {
      totalInventory += 1;
    }
  }
  console.log(totalInventory);
  return totalInventory;
}

console.log(inventoryScore('gold cup, puppy, magic cup, tooth of a magestic whale, tentacle of a giant squid, anything else'));
Enter fullscreen mode Exit fullscreen mode

Now we can see the issue clearly. When you do .split(',') it just breaks the string where commas have been found.
So given a string like 'gold cup, puppy, magic cup' you get an array like that:

['gold cup',' puppy',' magic cup']
Enter fullscreen mode Exit fullscreen mode

Notice the whitespace at the beginning of puppy and magic cup.

So a quick solution (and probably the best one) on that is to trim the items in the loop just like that:

function inventoryScore(inventory) {
  const splited = inventory.split(',');
  let totalInventory = 0;

  for (let i = 0; i < splited.length; i++) {
    // we trim the items before comparing them
    const item = splited[i].trim();
    if (item === 'gold cup') {
      totalInventory += 5;
    } else if (item === 'puppy') {
      totalInventory += 4;
    } else if (item === 'magic cup') {
      totalInventory += 10;
    } else if (item === 'tooth of a magestic whale') {
      totalInventory += 20;
    } else if (item === 'tentacle of a giant squid') {
      totalInventory += 100;
    } else if (item === 'anything else') {
      totalInventory += 1;
    }
  }
  return totalInventory;
}

console.log(inventoryScore('gold cup, puppy, magic cup, tooth of a magestic whale, tentacle of a giant squid, anything else'));
Enter fullscreen mode Exit fullscreen mode


Is a good practice though to split your logic in reusable chunks where it suits, document your code and so on.
I have refactored it quickly with best practices just as example:
// @ts-check

/**
 * Calculates the total weight of the inventory
 * @param {string} inventory
 * @returns {number}
 */
function inventoryScore(inventory) {
  let totalInventory = 0;

  inventory.split(',').forEach((item) => {
    totalInventory += getItemWeight(item.trim());
  });

  return totalInventory;
}

/**
 * Retrieves the weight of a given item
 * @param {string} item
 * @returns {number} weight of the item
 */
function getItemWeight(item) {
  if (item === 'gold cup') {
    return 5;
  } else if (item === 'puppy') {
    return 4;
  } else if (item === 'magic cup') {
    return 10;
  } else if (item === 'tooth of a magestic whale') {
    return 20;
  } else if (item === 'tentacle of a giant squid') {
    return 100;
  } else if (item === 'anything else') {
    return 1;
  }
  return 0;
}

console.log(inventoryScore('gold cup, puppy, magic cup, tooth of a magestic whale, tentacle of a giant squid, anything else'));
Enter fullscreen mode Exit fullscreen mode

Apologies if my function naming or function description in the JSDoc are not accurate within your software.

Hope it helps 😄

Collapse
 
laryken profile image
laryken

Thank you so much for your help. I really appreciate. I'm a beginner going through a Udemy course - The complete web development bootcamp by Angela Yu. Are you familiar with it? I hope i'm on the right track.

Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

I'm not much into Udemy courses but I can suggest you to complement it with freecodecamp, this way you can earn certificates as well :)

As suggestion I'd do this order:

  1. Responsive Web Design Certification
  2. JavaScript Algorithms and Data Structures Certification
  3. Front End Development Libraries Certification
  4. Back End Development and APIs Certification
  5. Relational Database Certification
  6. Quality Assurance Certification
  7. Information Security Certification
  8. Data Visualization Certification.

With this roadmap you can become a good full stack developer.

Just try to split the knowledge in some blocks and in a correct learning order, like that:

Frontend

  1. Responsive Web Design Certification
  2. JavaScript Algorithms and Data Structures Certification
  3. Front End Development Libraries Certification

Then do a couple of frontend projects (using public APIs) by yourself with the knowledge you gathered in those 3. I suggest you to use React and forget about Angular, Vue etc, the market share of react is much higher hence you'll probably get a job faster.

npmtrends.com/@angular/core-vs-rea...

Backend, then some valuable Data knowledge

  1. Back End Development and APIs Certification
  2. Relational Database Certification

Then do a backend project by yourself with the knowledge you gathered in those 3 (APIs, Services...).

At this point you can try using Next JS which is a Framework that uses Node and React and introduce yourself to SSR, SSG and similar concepts.

Testing and Security

  1. Quality Assurance Certification
  2. Information Security Certification

Almost nobody will hire you for your knowledge on those three but they are a good complement to the knowledge of the ones before and it can be a good boost on your career.

Extra experience working with different libraries

  1. Data Visualization Certification.

This one is just an extra certificate for the collection, you can be in a project where you need this or not. Even having the need, usually going to the documentation of a given library such D3 is enough to understand what to do plus if you are in a team you can get help from them.



If you just want to become a FrontEnd developer then the order is as follows:
  1. Responsive Web Design Certification
  2. JavaScript Algorithms and Data Structures Certification
  3. Front End Development Libraries Certification
  4. Quality Assurance Certification
  5. Data Visualization Certification.

And you can get the security one as extra knowledge after those.

Hope you find this useful.
If you need more detail I can make a post on that as it seems to be a common question.