Why did my code worked even through all ratings were strings and not numbers?

Tell us what’s happening:
The description says “Note that the rating values are saved as strings in the object and you may need to convert them into numbers to perform mathematical operations on them.” yet my code still worked.

What is the general rule for mathematical functions on values that are numbers stored as strings as opposed to values that are numbers?

Your code so far

const titRat = watchList.map(item =>({
  title: item["Title"],
  rating: item["imdbRating"]

const filteredList = titRat.filter(item =>item.rating>=8)

Can you link the challenge?

it depends on the mathematical operation
JavaScript will automatically convert stuff, but you need to check that it is doing the right behaviour.

In this case that you are using >= the strings are converted to numbers, and everything is fine.

If you for example had to take an average, which I think is what this challenge was about (and I think that part was removed), you can’t just sum things, you can try: '8.15' + 9 this will give '8.159'

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.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


1 Like