`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)
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:
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: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:
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:
Apologies if my function naming or function description in the JSDoc are not accurate within your software.
Hope it helps 😄
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.
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:
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
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
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
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
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:
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.