Search code examples
javascriptprototypejs

Javascript inheritance - asking experts oppinion on the following code


A friend of mine is attending a JavaScript course and thinks that the code he is submitting for grading is correct. However, the grader support keeps reporting it as not correct. He asked for my help and I tested the code on several IDEs and editors, online and offline, and I also got back every time a correct evaluation. However I don't use often JavaScript ans I'm hesitating to answer my friend that he is right. I would be most grateful if someone with more experience could tell me if the code evaluates correctly or not. Thank you.

"Determines which day of the week had the most nnumber of people visiting the pet store.
If more than one day of the week has the same, highest amount of traffic, an array containing the days (in any order) should be returned.
(ex. ["Wednesday", "Thursday"]).
If the input is null or an empty array, the function should return null.
@param week an array of Weekday objects
@return a string containing the name of the most popular day of the week if there is only one most popular day, and an array of the strings containing the names of the most popular days if there are more than one that are most popular"

function Weekday (name, traffic) {
    this.name = name;
    this.traffic = traffic;
}

function mostPopularDays(week) {
    // IMPLEMENT THIS FUNCTION!
    this.week = week;
    if (typeof week !== 'object' || week === null || week === undefined || week.length === 0) {
        return null;
    }

    var maxTr = 0;
    var maxTrDay = [];
    for (var i = 0; i < this.week.length; i++) {
        if (this.week[i].traffic > maxTr) {
            maxTrDay = [this.week[i].name];
            //maxTrDay = this.week[i].name;
            maxTr = this.week[i].traffic;
        } else if (this.week[i].traffic === maxTr) {
            //maxTrDay = [this.week[i].name];
            maxTrDay.push(this.week[i].name);
        } else if (this.week.length > 7) {
            this.week.shift();
        }
    }
    if (maxTrDay.length === 1) {
        console.log("The most popular day of the week was:")
        return maxTrDay[0];
    } else if (maxTrDay > 1) {
        console.log("The most popular days of the week were:")
        return maxTrDay;
    }
    return null;
}

The test case that the grader reports as failed are the following:
1. mostPopularDays should return an array of days when more than one day has most popular traffic

I used the following lines for testing, and the output was always the last (commented) line below:

var week = [];
var sun = new Weekday('Sunday', 100); week.push(sun);
var mon = new Weekday('Monday', 90); week.push(mon);
var tue = new Weekday('Tuesday', 100); week.push(tue);
mostPopularDays(week);
// [Sunday, Tuesday]

Solution

  • The issue is (maxTrDay > 1) is comparing an array object with the number 1. This will be false for all array inputs except for, confusingly, e.g. ([2] > 1), but that's JS for you.

    Running your code as-is with the provided driver (with added quotes to Tuesday to avoid a ReferenceError) yields the output of null.

    Your friend probably means (maxTrDay.length > 1), which compares based on length and yields the correct output:

    The most popular days of the week were:
    => [ 'Sunday', 'Tuesday' ]