繁体   English   中英

如何获取一周中每一天的密码?

[英]How can I get a password for each day of the week?

我正在参加训练营招聘,其中一项练习是获取一周中每一天的密码。 每周通行证总是相同的,但每天我必须 append 将工作日的辅音转换为每周通行证。 这是我写的。

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;
    }```

解决此问题的更简单方法是使用Date#getDay()获取一周内当天的0-6索引,并将后缀存储在具有相同索引的数组中。

然后使用天索引来检索相关的后缀。

 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 )

或者只是这个

 const currentPass = "aaa", weekDay = new Date().getDay(), // 0-6 (sun-mon) dayPass = currentPass + ['snd', 'mnd', 'tsd', 'wdnsd', 'thrsd', 'frd', 'strd'][weekDay]; console.log(dayPass)

考虑到你写的内容,让它工作的第一步是修复switch 语法currentPass的分配,如下所示:

 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM