I'm trying to use 'typeof'

Hello,
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/use-the-filter-method-to-extract-data-from-an-array
I’m tying to use ‘parseFloat’ to transform imdbRating to number but typeof return always string :face_with_thermometer:
What’s wrong ?

// The global variable
var watchList = [
  {
    "Title": "Inception",
    "Year": "2010",
    "Rated": "PG-13",
    "Released": "16 Jul 2010",
    "Runtime": "148 min",
    "Genre": "Action, Adventure, Crime",
    "Director": "Christopher Nolan",
    "Writer": "Christopher Nolan",
    "Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page, Tom Hardy",
    "Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
    "Language": "English, Japanese, French",
    "Country": "USA, UK",
    "Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
    "Metascore": "74",
    "imdbRating": "8.8",
    "imdbVotes": "1,446,708",
    "imdbID": "tt1375666",
    "Type": "movie",
    "Response": "True"
  },
  {
    "Title": "Interstellar",
    "Year": "2014",
    "Rated": "PG-13",
    "Released": "07 Nov 2014",
    "Runtime": "169 min",
    "Genre": "Adventure, Drama, Sci-Fi",
    "Director": "Christopher Nolan",
    "Writer": "Jonathan Nolan, Christopher Nolan",
    "Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
    "Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
    "Language": "English",
    "Country": "USA, UK",
    "Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
    "Metascore": "74",
    "imdbRating": "8.6",
    "imdbVotes": "910,366",
    "imdbID": "tt0816692",
    "Type": "movie",
    "Response": "True"
  },
  {
    "Title": "The Dark Knight",
    "Year": "2008",
    "Rated": "PG-13",
    "Released": "18 Jul 2008",
    "Runtime": "152 min",
    "Genre": "Action, Adventure, Crime",
    "Director": "Christopher Nolan",
    "Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
    "Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
    "Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
    "Language": "English, Mandarin",
    "Country": "USA, UK",
    "Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
    "Metascore": "82",
    "imdbRating": "9.0",
    "imdbVotes": "1,652,832",
    "imdbID": "tt0468569",
    "Type": "movie",
    "Response": "True"
  },
  {
    "Title": "Batman Begins",
    "Year": "2005",
    "Rated": "PG-13",
    "Released": "15 Jun 2005",
    "Runtime": "140 min",
    "Genre": "Action, Adventure",
    "Director": "Christopher Nolan",
    "Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
    "Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
    "Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
    "Language": "English, Urdu, Mandarin",
    "Country": "USA, UK",
    "Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
    "Metascore": "70",
    "imdbRating": "8.3",
    "imdbVotes": "972,584",
    "imdbID": "tt0372784",
    "Type": "movie",
    "Response": "True"
  },
  {
    "Title": "Avatar",
    "Year": "2009",
    "Rated": "PG-13",
    "Released": "18 Dec 2009",
    "Runtime": "162 min",
    "Genre": "Action, Adventure, Fantasy",
    "Director": "James Cameron",
    "Writer": "James Cameron",
    "Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
    "Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
    "Language": "English, Spanish",
    "Country": "USA, UK",
    "Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
    "Metascore": "83",
    "imdbRating": "7.9",
    "imdbVotes": "876,575",
    "imdbID": "tt0499549",
    "Type": "movie",
    "Response": "True"
  }
];

// Only change code below this line
let selectFilms = watchList.filter( film => film.imdbRating > 8.0);
//console.log(selectFilms)
selectFilms.forEach(item => parseFloat(item.imdbRating))
let test = selectFilms[0].imdbRating;
console.log(test)
console.log(typeof test)
/*let filteredList = selectFilms.map(function (film) {
  return {
      "Title": film["Title"],
      "imdbRating": film["imdbRating"]
    }
  
})*/
/*let filteredList = selectFilms.map( film => ({"Title": film["Title"], "imdbRating": film["imdbRating"]}))*/

/*let filteredList = selectFilms.map(({Title: title, imdbRating: rating}) => ({title, rating}));*/

//var filteredList;

// Only change code above this line

//console.log(filteredList);

console.log:
image

What is this line doing?

1 Like

I’m not sure this is doing what you want it to do. What is your intent here? The forEach method iterates through each element in an array but it doesn’t do anything else. If you want to do something then you have to do it in the callback function. The only thing you are doing in the callback is calling parseFloat. But that in itself doesn’t really do anything.

‘8.8’ is selectFilms[film].indbRating
then
parseFloat(‘8.8’) should return the number 8.8
then
I whant do this for all films so in selectFilm
forEach allow me to iterate each film in selectFilm.

I tried with

selectFilms.forEach(parseFloat(imdbRating))

does not work either. :upside_down_face:

It’s the reason why I added a function whit item argument

parseInt doesn’t modify its argument…

I would take a hint from the instructions and just use the map and filter methods to solve this. You don’t need forEach at all. Remember, filter and map have “forEach” built into them.

Right. If you have a String (for example “3”), parseFloat(“3”) does return the float 3. But if you have, say:
let x = “3”
parseFloat(x) // returns 3
console.log(x) // prints “3”
What’s going on is that parseFloat does not transform the value into a float globally, just right there where you are calling it. The forEach( ) method isn’t saving the new values anywhere. For that you might try one of the other Array methods such as .map( )

I know. I did the execise. It’s work well.
I just wonder how to transform imdbRating to numer

How could you complete the exercise if you don’t know how to do this? One of the requirements is a numerical comparison.

Update: OK, I guess you were probably relying on implicit conversions to do this. I would make sure you understand how that works because JS has the tendency to do conversions you don’t want as well :slight_smile:

For example, "8.1" > 8.0 will give you want you want but "8.1" + 8.0 is a classic source of bugs.

Sidenote: I guess it’s just the C/C++ in me but when I solved this I did Number(film.imdbRating) > 8.0 even though you don’t technically have to. Old habits are hard to break. And ya, I could have done +film.imdbRating like the cool kids but I’m not that hip.

1 Like

You only need to use parseFloat where you need the number. Sticking it into a forEach does absolutely nothing for you.

yes i’m trying with parseInt…

Your parseFloat is not where you are using the number. It is stuck in a random forEach that has no connection with any other code.

This line right here says 'loop over the array selectFilms, get the imdbRating from each item, parse the string as a number, and do absolutely nothing with the result, throwing it away into the vast void of nothingness where unused return values go`.


But that problem aside, @bbsmooth is 100% correct; you don’t even need to parse the string as a number for the solution to work just fine.

I know I already done the exercise. Look at the bottom of code all lines with //

Ok… did you read the other half of what I posted? Do you understand what we have been saying? Calling parseFloat in a forEach has no effect on the item.imdbRating.

It’s ok if you don’t understand, but please ask questions if that’s the case.

How would you do in order typeOf displays ‘number’ ?

The most idiomatic way would be to do it as part of your map.

Ok,

Because in the exercise statement :

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.

So I beleived it was necessary to convert string value to number BEFORE to realise the calcul with filter(). I succeeded to do that with map, but that’s not good in order to pass the test because at the end we must have all values as string.

// The global variable
var watchList = [
  {
    "Title": "Inception",
    "Year": "2010",
    "Rated": "PG-13",
    "Released": "16 Jul 2010",
    "Runtime": "148 min",
    "Genre": "Action, Adventure, Crime",
    "Director": "Christopher Nolan",
    "Writer": "Christopher Nolan",
    "Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page, Tom Hardy",
    "Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
    "Language": "English, Japanese, French",
    "Country": "USA, UK",
    "Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
    "Metascore": "74",
    "imdbRating": "8.8",
    "imdbVotes": "1,446,708",
    "imdbID": "tt1375666",
    "Type": "movie",
    "Response": "True"
  },
  {
    "Title": "Interstellar",
    "Year": "2014",
    "Rated": "PG-13",
    "Released": "07 Nov 2014",
    "Runtime": "169 min",
    "Genre": "Adventure, Drama, Sci-Fi",
    "Director": "Christopher Nolan",
    "Writer": "Jonathan Nolan, Christopher Nolan",
    "Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
    "Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
    "Language": "English",
    "Country": "USA, UK",
    "Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
    "Metascore": "74",
    "imdbRating": "8.6",
    "imdbVotes": "910,366",
    "imdbID": "tt0816692",
    "Type": "movie",
    "Response": "True"
  },
  {
    "Title": "The Dark Knight",
    "Year": "2008",
    "Rated": "PG-13",
    "Released": "18 Jul 2008",
    "Runtime": "152 min",
    "Genre": "Action, Adventure, Crime",
    "Director": "Christopher Nolan",
    "Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
    "Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
    "Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
    "Language": "English, Mandarin",
    "Country": "USA, UK",
    "Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
    "Metascore": "82",
    "imdbRating": "9.0",
    "imdbVotes": "1,652,832",
    "imdbID": "tt0468569",
    "Type": "movie",
    "Response": "True"
  },
  {
    "Title": "Batman Begins",
    "Year": "2005",
    "Rated": "PG-13",
    "Released": "15 Jun 2005",
    "Runtime": "140 min",
    "Genre": "Action, Adventure",
    "Director": "Christopher Nolan",
    "Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
    "Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
    "Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
    "Language": "English, Urdu, Mandarin",
    "Country": "USA, UK",
    "Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
    "Metascore": "70",
    "imdbRating": "8.3",
    "imdbVotes": "972,584",
    "imdbID": "tt0372784",
    "Type": "movie",
    "Response": "True"
  },
  {
    "Title": "Avatar",
    "Year": "2009",
    "Rated": "PG-13",
    "Released": "18 Dec 2009",
    "Runtime": "162 min",
    "Genre": "Action, Adventure, Fantasy",
    "Director": "James Cameron",
    "Writer": "James Cameron",
    "Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
    "Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
    "Language": "English, Spanish",
    "Country": "USA, UK",
    "Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
    "Metascore": "83",
    "imdbRating": "7.9",
    "imdbVotes": "876,575",
    "imdbID": "tt0499549",
    "Type": "movie",
    "Response": "True"
  }
];

// Only change code below this line

let newWatchList = watchList.map( film => ({"title": film["Title"], "rating": Number(film["imdbRating"])}));
let test = newWatchList[0].rating;
console.log(test)
console.log(typeof test)
let filteredList = newWatchList.filter( film => film.rating > 8.0);

// Only change code above this line

console.log(filteredList);

image

So, I well understand why parseFloat is used with filter(). paseFloat demande to deal the value as number but doesn’t change physicaly the value in the Array.

So to pass the test we have to do that in the filter. in this way the values remain as strings in the array.

To end, if I replace Number(film["imdbRating"]) by parseInt (film["imdbRating"]) that’s doesn’t works because it return the integer 8 & 9. then the filter return only one movie with rate 9. I would have to change “>8.0” by “>=8.0”.

Sorry if I’wasn’t clear enougth. Maybe I’m asking myself too many questions but I’m trying to understand in depth each exercice before before going any further. :wink:

Ya, that note is slightly misleading. Technically, yes, you do need to convert a string representing a number to an actual number before you can compare it to another number. But JS will do this conversion for you automatically for most operators (this is referred to as implicit conversion or type coercion). Thus, all of the following statements will give you the same answer because they all convert the numeric string to an actual number before doing the comparison:

Number("8.2") > 8.1
parseFloat("8.2") > 8.1
+"8.2" > 8.1
"8.2" > 8.1

The last one is taking advantage of implicit conversion.

So the note is perhaps wrong when it says “you may need to convert them into numbers” because you indeed do have to convert them into numbers before you can compare them to another number. You just don’t need to do this explicitly because you can rely on JS to do it for you.

I will note that not everyone is a fan of implicit conversion and they would recommend that you always explicitly convert your values.

1 Like

I generally prefer my languages to demand that I explicitly typecast when making a comparison between dissimilar data.

1 Like

Thank you all, it time to sleep for me.
See you tomorrow, maybe.