Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

349
Vistas
Write a program which tells the number of days in a month. consider leap year

I want to write a script in Js which tells number of days in a month and the test case must satisfy the below:

Enter a month: January. output-> January has 31 days.

Enter a month: JANUARY output-> January has 31 day

Enter a month: February output-> February has 28 days.

Enter a month: FEbruary output-> February has 28 days.

and my code is:

let a = prompt('Enter the month:');

let b = a.charAt(0).toUpperCase();

let c = a.slice(1, a.length).toLowerCase();

let Month = (b + c);

if ('January' === Month || 'March' === Month || 'May' === Month || 'July' === Month || 'Agust' === Month || 'October' === Month || 'December' === Month) {
console.log(`${Month} has 31 days`)
}
else if ('February' === Month) {
console.log(`${Month} has 28 days`);
} 
else if ('April' === Month || 'June' === Month || 'September' === Month || 'November' === Month) {
   console.log(`${Month} has 30 days`)
}
else{
console.log('Re-Enter');
}

It satisfies only first test case anybody help me out with the correct logic which satisfies all the test cases.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can calculate retrieve the number of days for a month in any year by retrieving the date value of a Date for the next month with a date value of 0. Here's a small factory to determine the numbers of day per month (numeric or using a month name):

const getNDaysForMonthFactory = _ => {
  const months = (`january,february,march,april,may,june,`+
    `july,august,september,october,november,december`).split(',');
  const byNr = (year, month) => new Date(year, month, 0).getDate();
  
  return {
    byNr, 
    byName: (year, month) => {
      const m = months.findIndex( m => m === month.toLowerCase());

      return m > -1 
        ? byNr(year, m + 1) 
        : `[${month}] is not a valid month`;
      },
   };
}

const {byNr, byName} = getNDaysForMonthFactory();

console.log(`(byNr) 6 2022: ${byNr(2022, 6)} days`);
console.log(`(byNr) 2 2022: ${byNr(2022, 2)} days`);
console.log(`(byNr) 2 2000: ${byNr(2000, 2)} days`);
console.log(`(byName) february 2000: ${byName(2000, `FEBRuary`)} days`);
console.log(`(byName) nothing 2022: ${byName(2022, `nothing`)}`);

// so
showDays();
document.addEventListener(`change`, handle);
document.addEventListener(`keyup`, handle);

function showDays() {
    const [month, year] = [
      +document.querySelector(`#month`).value,
      +document.querySelector(`#year`).value ];
    document.querySelector(`#nDays`).textContent = 
      `${byNr(year, month)} days`;
}

function handle(evt) {
  if (evt.target.id === `year` || evt.target.id === `month`) {
    return showDays();
  }
}
<select id="month">
  <option value="1" selected>January</option>
  <option value="2">February</option>
  <option value="3">March</option>
  <option value="4">April</option>
  <option value="5">May</option>
  <option value="6">June</option>
  <option value="7">July</option>
  <option value="8">August</option>
  <option value="9">September</option>
  <option value="10">October</option>
  <option value="11">November</option>
  <option value="12">December</option>
</select> 
<input id="year" type="number" value="2022"> <span id="nDays"></span>

about 4 years ago · Juan Pablo Isaza Denunciar

0

This is a modified version of @KooiInc response without the while loop

const getNDays = (year, month) => {
  let daysOfMonth = 0;
  let date = new Date(Date.UTC(year, month, 1, 0, 0, 0));
  date.setDate(date.getDate() - 1);
  
  
  return date.getDate();
}

console.log(`june 2022: ${getNDays(2022, 6)} days`);
console.log(`february 2022: ${getNDays(2022, 2)} days`);
console.log(`february 2000: ${getNDays(2000, 2)} days`);
console.log(`february 1900: ${getNDays(1900, 2)} days`);

// so
showDays();
document.addEventListener(`change`, handle);

function showDays() {
    const [month, year] = [
      +document.querySelector(`#month`).value,
      +document.querySelector(`#year`).value ];
    document.querySelector(`#nDays`).textContent = 
      `${getNDays(year, month)} days`;
}

function handle(evt) {
  if (evt.target.id === `year` || evt.target.id === `month`) {
    return showDays();
  }
}
<select id="month">
  <option value="1" selected>January</option>
  <option value="2">February</option>
  <option value="3">March</option>
  <option value="4">April</option>
  <option value="5">May</option>
  <option value="6">June</option>
  <option value="7">July</option>
  <option value="8">August</option>
  <option value="9">September</option>
  <option value="10">October</option>
  <option value="11">November</option>
  <option value="12">December</option>
</select> 
<input id="year" type="number" value="2022"> <span id="nDays"></span>

about 4 years ago · Juan Pablo Isaza Denunciar

0

We can create a formatDaysInMonth() function that will take a monthName such as January, FEBRUARY, etc and a year and return the formatted month and days, like "February 2020 has 29 days":

function monthNameToMonth(monthName) {
  const months = { january: 1, february: 2, march: 3, april: 4, may: 5, june: 6, july: 7, august: 8, september: 9, october: 10, november: 11, december: 12 };
  return months[monthName.toLowerCase()]
}

function getDaysInMonth(year, month) {
  return new Date(year, month, 0).getDate();  
}

function formatDaysInMonth(year, monthName) {
  let month = monthNameToMonth(monthName);
  let days = getDaysInMonth(year, month);
  monthName = monthName.toUpperCase().slice(0,1) + monthName.toLowerCase().slice(1);
  return `${monthName} ${year} has ${days} days`;
}

let monthName = prompt('Enter the month name:');
let year = prompt('Enter the year:');
console.log(formatDaysInMonth(year, monthName))
.as-console-wrapper { max-height: 100% !important; }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda