Im on a bootcamp recrtuitment , and one of the exercises is to get a password for each day of the week . The weekly pass is allways de same but in each day I have to append the consonants of the weekday to the weeklyPass . Here is what I wrote .
var weekDay = 'Friday';
var currentPass; // this is the values they gave me
switch (weekDay) {
case (weekDay = 'monday'):
console.log(currentPass = weeklyPass 'mnd');
break;
case (weekDay = 'tuesday'):
console.log(currentPass = weeklyPass 'tsd');
break;
case (weekDay = 'wednesday'):
console.log(currentPass = weeklyPass 'wdnsd');
break;
case (weekDay = 'thursday'):
console.log(currentPAss = weeklyPass 'thrsd');
break;
case (weekDay = 'friday'):
console.log(currentPass = weeklyPass 'frd');
break;
case (weekDay = 'saturday'):
console.log(currentPass = weeklyPass 'strd');
break;
case (weeKday = 'sunday'):
console.log(currentPass = weeklyPass 'snd');
break;
}```
CodePudding user response:
A simpler way to approach this would be to use Date#getDay() to get the 0-6
index of the current day within the week and store the suffixes in an array with same indexing.
Then use the day index to retrieve the associated suffix.
const suffixes = ['snd','mnd','tsd','wdnsd','thrsd','frd','strd']
const weekPass = 'ABC_';
const day = (new Date()).getDay();
const pass = weekPass suffixes[day];
console.log('Day index =',day, ', Passs =',pass )
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
Or just this
const currentPass = "aaa",
weekDay = new Date().getDay(), // 0-6 (sun-mon)
dayPass = currentPass ['snd', 'mnd', 'tsd', 'wdnsd', 'thrsd', 'frd', 'strd'][weekDay];
console.log(dayPass)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
Considering what you wrote, the first step to get it working would be to fix the switch syntax and the assignment of currentPass
like this:
var weekDay = 'Friday';
var currentPass = "this_week_password"; // this is the values they gave me
switch (weekDay) {
case 'Monday':
currentPass = 'mnd';
break;
case 'Tuesday':
currentPass = 'tsd';
break;
case 'Wednesday':
currentPass = 'wdnsd';
break;
case 'Thursday':
currentPass = 'thrsd';
break;
case 'Friday':
currentPass = 'frd';
break;
case 'Saturday':
currentPass = 'strd';
break;
case 'Sunday':
currentPass = 'snd';
break;
}
console.log(currentPass)
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>