2021年3月19日星期五

Is there a better way to do this code? Perhaps using filter or reduce?

The code I have is correct, I'm just curious if there is a shorter way to do this? I think filter would be the best method but I'm not so sure how to use it.

function tallEnoughToRide(group) {    var result = [];    for (var i = 0; i < group.length; i++) {        if (group[i].heightInInches >= 48) {            result.push(group[i].name);        }    }    return result; //should return a list of people taller than 48 inches  }    var groupA = [    {      name: "Mia",      age: 10,      heightInInches: 52    },    {      name: "Jaya",      age: 9,      heightInInches: 45    },    {      name: "Kiana",      age: 10,      heightInInches: 55    },    {      name: "Alex",      age: 11,      heightInInches: 48    }  ];  console.log(tallEnoughToRide(groupA));  
https://stackoverflow.com/questions/66717374/is-there-a-better-way-to-do-this-code-perhaps-using-filter-or-reduce March 20, 2021 at 09:11AM

没有评论:

发表评论