我的挑战说明是这样的:
"使用WHILE循环,编写一个函数imabouttoexplodewitheexciting,从n开始打印倒计时。当倒计时到5时,打印'哦,哇,我无法处理预期!'当它是3时,打印"我兴奋得要爆炸了!"当计数器完成时,打印"That was kind a let down"。
我已经成功地解决了第一部分并打印了字符串"那是一种让人失望的"倒计时结束
我的问题是我不知道如何使用if/else条件将某些迭代中的数字替换为字符串(数字3和5)。
我知道它涉及到条件句,但我根本不知道这样的短语是什么样子的。
谢谢。
function imAboutToExplodeWithExcitement(n){
//declare variable countdown
let countDown = n
// using a while loop, decrement from the value of n to 0
while( countDown >= 0) {
console.log(countDown);
countDown--;
// if-else statements to replace 3 and 5 with their respective strings...
if
}
//print message marking the end of the countdown
console.log("That was kind of a let down.");
}
imAboutToExplodeWithExcitement(10); // expected log 10, 9, 8, 7, 6, 'Oh wow, I can't handle the anticipation!', 4, I'm about to explode with excitement!', 2, 1, 'That was kind of a let down'
我写了这个,它似乎像你想要的那样工作!这段代码的输出将打印数字10并一直到0,但是对于数字5、3和0,它将替换这些数字并打印出一个特定的字符串!
function imAboutToExplodeWithExcitement(n) {
while (n >= 0) {
if (n > 5) console.log(n)
else if (n === 5) console.log(`Oh wow, I can't handle the anticipation!`)
else if (n === 3) console.log(`I'm about to explode with excitement!`)
else if (n === 0) console.log(`That was kind of a let down`)
else if (n > 0) console.log(n)
n--
}
}
function imAboutToExplodeWithExcitement(n){
while(n){
if(n === 5){
console.log("Oh wow, I can't handle the anticipation!");
}else if(n === 3){
console.log("I'm about to explode with excitement!");
}else{
console.log(n);
}
n--;
}
console.log('That was kind of a let down');
}
这段代码解释了它自己。希望能有所帮助。
function imAboutToExplodeWithExcitement(n){
while (n > 0) {
console.log(n);
n--;
if (n === 5) console.log('Oh wow, I can't handle the anticipation!');
else if (n === 3) console.log('I'm about to explode with excitement!');
else if (n === 0) console.log('That was kind of a let down');
}
}
imAboutToExplodeWithExcitement(10);