-2
/**
 * A prototype to create Weekday objects
 */


  function Weekday (name, traffic) {
        this.name = name;
        this.traffic = traffic;
    }
//function to return the day that has the most traffic.
function mostPopularDays(week) {
    // IMPLEMENT THIS FUNCTION!

}
//this is my input
var a = new Weekday('mon',123); var b = new Weekday('tues', 134) ; var c = new Weekday('wed', 233);
empty_me = []; 
empty_me.push(a ,b ,c);
mostPopularDays(empty_me);
// it should return the day with most traffic
//e.g wed

This code is collecting input as an array with objects embedded inside it... the function 'mostPopularDays' should return a string of the name of the day with the most traffic or an array with the names of the days with the most traffic.

4 Answers4

0

You can use the reduce array method to do this:

function mostPopularDays(week) {
    let mostTrafficInADay = week.reduce((acc, val) => {
        acc = ( val.traffic > acc ) ? val.traffic : acc;
        return acc;
    }, 0);
    return week.filter(day => day.traffic === mostTrafficInADay).map(day => day.name);
}

This will return an array containing strings of all of the names of the days where the traffic was the highest.

Edit: since the question changed, it also uses the array map and filter methods.

markmoxx
  • 1,333
  • 1
  • 9
  • 19
  • please check out my edited post – Stanley Luke Jan 07 '19 at 12:01
  • @StanleyLuke: Just noticed you moved the goalposts, you only want the name of the day with the most traffic. Changed the function to reflect this. – markmoxx Jan 07 '19 at 12:06
  • Thanks a lot, Please what if we have more than one day with same traffic, can you share a code to return the days as an array. eg [thurs,fri] – Stanley Luke Jan 07 '19 at 12:16
  • I was going to ask you what you want to happen in this case. I'll do this for you, but just know that constantly changing the goalposts of a question is not good SO etiquette. – markmoxx Jan 07 '19 at 12:17
  • Thanks, waiting – Stanley Luke Jan 07 '19 at 12:24
  • @StanleyLuke it might be worthwhile to give this a read: https://medium.com/jsguru/javascript-functional-programming-map-filter-and-reduce-846ff9ba492d – markmoxx Jan 07 '19 at 12:28
  • @StanleyLuke I just updated the answer as it had a mistake in it - the accumulator of the `reduce` function needs to be 0 to start with. – markmoxx Jan 07 '19 at 12:39
  • @StanleyLuke You changed the question again! Do you really want to output a string when there's only one day, and an array when there's more than one day? That will just make your life harder. Just working with an array, regardless of how many days are in it, will simplify things a lot for you. Otherwise you would need to do extra type checking when working with the value returned from `mostPopularDays()`. – markmoxx Jan 07 '19 at 12:45
0

It is a bit long but first you can find max value then find relevant items in list matching

var wdArr = [ {name: "mon", traffic: 123}, {name: "tue", traffic: 120}, {name: "wed", traffic: 124}, {name: "thurs", traffic: 455}]



resInt = Math.max.apply(Math, wdArr.map(function(o) { return o.traffic; }))

resObj = wdArr.filter (function(el){

return el.traffic == resInt
});

console.log(resObj)
asdf_enel_hak
  • 7,114
  • 3
  • 36
  • 77
0

You can compare the traffic value in the array#redcue and get the height valued traffic object.

let data = [{name: "mon", traffic: 123}, {name: "tue", traffic: 120}, {name: "wed", traffic: 124}, {name: "thurs", traffic: 455}],
    result = data.reduce((r,o) => r.traffic < o.traffic ? o : r);
console.log(result);
Hassan Imam
  • 16,414
  • 3
  • 29
  • 41
0

You can use the reduce() method.

The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value.

var weekdays = [
  {name: "mon", traffic: 123},
  {name: "tue", traffic: 120}, 
  {name: "wed", traffic: 124}, 
  {name: "thu", traffic: 455},
  {name: "fri", traffic: 455},
]


var maxTrafficWeekday = weekdays.reduce(function (acc, val) {
  return val.traffic > acc.traffic ? val : acc
})

console.log(maxTrafficWeekday)

The above code will return the first day which has most traffic. If you want to get the last day with most traffic, you can change the algorithm:

var weekdays = [
  {name: "mon", traffic: 123},
  {name: "tue", traffic: 120}, 
  {name: "wed", traffic: 124}, 
  {name: "thu", traffic: 455},
  {name: "fri", traffic: 455},
]


var maxTrafficWeekday = weekdays.reduce(function (acc, val) {
  return val.traffic >= acc.traffic ? val : acc
  // -----------------^
})

console.log(maxTrafficWeekday)
dashtinejad
  • 5,979
  • 3
  • 25
  • 42