0

Beginner JS programmer here(3 weeks): Not sure if my title accurately conveys my problem but I'll print it out below. I was trying to get the longest name(title) in my array. I created the variable longestName which returns just the titles in the array as desired. I then wanted to use a reducer to reduce the array based on length but I received an error. I know the computer isn't wrong and I am sure there is a way to do this, I just haven't been able to put my finger on it yet so here I am.

const movieReviews = [
    {
       title : "Black Adam",
        score : 68,
        year : 2022
    },
    {
       title : "Black Panther",
       score : 99,
       year : 2018
    },
    {
       title : "SpiderMan FFH",
       score : 90,
       year : 2020
    },
    {
       title : "Avatar",
       score : 100,
       year : 2009
    },
    {
        title : "WonderWoman",
        score : 75,
        year : 2019
    },
]



const longestName = movieReviews.map(m =>{
    return m.title;
})
.reduce((name1, name2)=>{
    if(name1.length() > name2.length()){
        return name1;
    }
    return name2;
})



  • Learn about [how to debug small programs](//ericlippert.com/2014/03/05/how-to-debug-small-programs). [Rubber Duck Debug](//rubberduckdebugging.com) your code. Try using your browser’s [debug capabilities](//ali-dev.medium.com/how-to-easily-debug-in-javascript-5bac70f94f1a). See [What is a debugger and how can it help me diagnose problems?](/q/25385173/4642212). Use the [browser console (dev tools)](//webmasters.stackexchange.com/q/8525) (hit `F12`), read any errors. [`length`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/length) isn’t a method. – Sebastian Simon Dec 02 '22 at 20:41
  • 1
    Remove the `()` after length. `length` is a property, not a method. – Andrew Shearer Dec 02 '22 at 20:46
  • Thanks man! I knew it was gonna be something super obvious I just wasn't seeing – MonkeyDZoro Dec 02 '22 at 21:01

0 Answers0