JavaScript bootcamp · Lab 38

Days in a month

easySwitch10 minLesson: Switch

Read the question, write JavaScript on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read month 1–12; print days in a non-leap year.

Input. 1–12.

Output. 28/30/31.

Examples

Example 1
Input
2
Output
28
Example 2
Input
4
Output
30
Hint
  1. Array of day counts indexed by month.
Show correct code

Peek only after you have tried. You can still Check your own version.

const m = Number(readLine());
const days = [0,31,28,31,30,31,30,31,31,30,31,30,31];
console.log(days[m]);
main.jsconsole.log · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.