vendredi 6 novembre 2020

javascript function exercise day of the week

I need to make a function that takes an integer from 1 to 7.

if the number less than 1 or greater than 7 the function should return null. and then each number should represnt day of the week.

so if i try something like function returnDay(1) it should give me an output of 'Sunday' ,

2 > monday

3 > tuesday and so on..

so this is my approach, yet it didnt work and i think my syntax is a little bit sketchy so i would like to get the currect appraoch :) thx

const weekDAY= new Array(7);
weekDAY[1] = 'Sunday' ; 
weekDAY[2] = 'Monday';
weekDAY[3] = 'Tuesday';
weekDAY[4] = 'Wednesday';
weekDAY[5] = 'Thursday';
weekDAY[6] = 'Friday';
weekDAY[7] = 'Saturday';

function returnDay(x){
    return (x < 1) || (x > 7) ? null :
   } else { 
      let thisDay = weekDAY[x]
      return thisDay; 
  }

Aucun commentaire:

Enregistrer un commentaire