0

write a function(not a generator) called displayDayNumber(dateString) that satisfies the following rule.


var dayNumber =displayDayNumber("Nov 5,2017");  //309
console.log(dayNumber ()); // 310
dayNumber("Oct 31 2017"));  //304
dayNumber();//305

So far My code is working for one time dateString pass value but it is not calculating based when I am passing new value in dayNumber.


var dayNumber =function(dateString=new Date().toDateString())
{
var currentString=dateString;
var no=dateString.substring(8,10);
// console.log(no);

    return function () {
       // console.log("dateString=",dateString);
        var b = calculateNumber(dateString)   //helper function to calculate day No
        {

            var no = dateString.substring(8, 10);
            var toNo = parseInt(no) + 1;
            var thenString = toNo.toString() + ' ';
            var replacecharacter = dateString.replace(no, thenString);
            dateString = replacecharacter;
            return b;
        };
        return dateString;
    };

}

let genDayNumber=dayNumber ("Sat Nov 05 2017"); //309  //working 
genDayNumber();     //working
genDayNumber("Wed Oct 31 2017")); //304   not working
genDayNumber();     //not working
jMoore
  • 53
  • 8
  • Yikes, that’s an awful interface. Anyway, you’ll probably find it easiest to write a completely separate function that parses a date into a day number, reducing this weird thing into checking whether a string was passed and returning either `day = parseDay(string)` or `++day` accordingly. – Ry- Nov 05 '17 at 17:06
  • _"that satisfies the following rule."_ Can you provide further description of the "rule"? What is the requirement? – guest271314 Nov 05 '17 at 17:07
  • Where is `days` defined? You've got `let genDayNumber=days("Sat Nov 05 2017");`, but I can't see a `days` function – user184994 Nov 05 '17 at 17:08
  • @guest271314: It should satisfy both the condition means that if string is being passed then consider the previous date or else consider the current date value which is being passed. – jMoore Nov 05 '17 at 17:10
  • @d.aparna "consider" what and using what parameters? The actual text of Question displays expected output in code, though not in plain terms of what the pattern should return given a specific argument passed, or no argument passed, e.g, `genDayNumber()); //not working`; note the extra `)`. See https://stackoverflow.com/help/how-to-ask. – guest271314 Nov 05 '17 at 17:14

2 Answers2

0
const dayNumber = date => {
  let days = new Date(date).getDay();
  return arg => {
    if(arg){
       return days = new Date(arg).getDay();
    }
    return ++days;
  };
};
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

The function you're returning doesn't take an argument, so when you're passing in "Wed Oct 31 2017", it's simply ignored.

You need to modify the returned function to accept a value.

Here is a working example

var dayNumber =displayDayNumber("Nov 5,2017");  //309
console.log(dayNumber); // 309
console.log(dayNumber()); // 310

console.log(dayNumber("Oct 31 2017"));  //304
console.log(dayNumber())//305


function displayDayNumber(str) {
    let date = new Date(str);
    let day = dayOfYear(date);

    var result = function(str) {
        if (str) {
            day = displayDayNumber(str);
            return day;
        } else {
            return ++day;
        }
    }

    result.valueOf = result.toString = function() { return day; }
    return result;
}

function dayOfYear(now) {
    // Taken from https://stackoverflow.com/questions/8619879/javascript-calculate-the-day-of-the-year-1-366
    var start = new Date(now.getFullYear(), 0, 0);
    var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000); 
    var oneDay = 1000 * 60 * 60 * 24;
    var day = Math.floor(diff / oneDay);
    return day;
}
user184994
  • 17,791
  • 1
  • 46
  • 52