1

I was working on the below javascript code, could someone explain why below code output is undefined?

Given a year, find the date of the 256th day of that year according to the official Russian calendar during that year.

Then print it in the format dd.mm.yyyy.

function solve(year) {
  if (year < 1700 || year > 2700) {
    return false;
  } else if (year >= 1700 && year <= 1917) {
    if (year % 4 === 0) {
      var days = [31, 29, 31, 30, 31, 30, 31, 31];
      calculate(days)
      `enter code here`
    }
    var days = [31, 28, 31, 30, 31, 30, 31, 31];
    calculate(days)
  } else if (year > 1918 && year <= 2700) {
    if (year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) {
      var days = [31, 29, 31, 30, 31, 30, 31, 31];
      calculate(days)
    }
    var days = [31, 28, 31, 30, 31, 30, 31, 31];
    calculate(days)
  } else if (year === 1918) {
    var days = [31, 15, 31, 30, 31, 30, 31, 31];
    calculate(days)
  }

  function calculate(days) {
    var daysOfSep = 256 - days.reduce(function(accumulator, currentValue) {
      return accumulator + currentValue
    }, 0)

    return `${daysOfSep}.09.${year}`
  }
}

console.log(solve(1918))

3

2 Answers 2

2

Because you are not returning within your main function, by default a function expression returns undefined:

function solve(year){
        if(year < 1700 || year > 2700) {
          return false;
        }else if(year >= 1700 && year <= 1917) {
          if(year%4 === 0) {
            var days = [31,29,31,30,31,30,31,31];
            calculate(days)`enter code here`
      }
          var days = [31,28,31,30,31,30,31,31];
          calculate(days)
       }else if(year > 1918 && year <= 2700) {
         if(year % 400 === 0 || (year%4 === 0 && year%100 !== 0)) {
         var days = [31,29,31,30,31,30,31,31];
         calculate(days)
       }
      var days = [31,28,31,30,31,30,31,31];
      return calculate(days) //calculate inner function returns the result, but you do not return this.
    }else if(year === 1918) {
      var days = [31,15,31,30,31,30,31,31];
      return calculate(days) //calculate inner function returns the result, but you do not return this.
    }

        function calculate (days) {
          var daysOfSep = 256 - days.reduce(function(accumulator, currentValue) {
           return accumulator + currentValue
          },0)

          return `${daysOfSep}.09.${year}`
        }
    }

    console.log(solve(1918))
 26.09.1918
Sign up to request clarification or add additional context in comments.

Comments

1

As you are calling solve() function first and inside of solve() function again you are calling calculate() function. You have return value only inside of calculate() function not in solve() function that's reason you are getting undefined

Your code should be like this.

DEMO

function solve(year) {
  if (year < 1700 || year > 2700) {
    return false;
  } else if (year >= 1700 && year <= 1917) {
    if (year % 4 === 0) {
      var days = [31, 29, 31, 30, 31, 30, 31, 31];
      calculate(days)
      `enter code here`
    }
    var days = [31, 28, 31, 30, 31, 30, 31, 31];
    return calculate(days)
  } else if (year > 1918 && year <= 2700) {
    if (year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) {
      var days = [31, 29, 31, 30, 31, 30, 31, 31];
     return  calculate(days)
    }
    var days = [31, 28, 31, 30, 31, 30, 31, 31];
    calculate(days)
  } else if (year === 1918) {
    var days = [31, 15, 31, 30, 31, 30, 31, 31];
   return  calculate(days)
  }

  function calculate(days) {
    var daysOfSep = 256 - days.reduce(function(accumulator, currentValue) {
      return accumulator + currentValue
    }, 0)

    return `${daysOfSep}.09.${year}`
  }
}

console.log(solve(1918))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.