【发布时间】:2022-08-15 16:21:17
【问题描述】:
我有一个初学者的 JavaScript 练习,当前代码如下所示:
// Task 1: Build a function-based console log message generator
function consoleStyler(color, background, fontSize, txt) {
var message = \"%c\" + txt;
var style = `color: ${color};`
style += `background: ${background};`
style += `font-size: ${fontSize};`
console.log(message, style);
}
// Task 2: Build another console log message generator
function celebrateStyler(reason) {
var fontStyle = \"color: tomato; font-size: 50px\";
if (reason == \'birthday\') {
console.log(`%cHappy Birthday`, fontStyle);
} else if (reason == \"champions\") {
console.log(`%cCongrats on the title!`, fontStyle);
} else {
console.log(message, style);
}
}
// Task 3: Run both the consoleStyler and the celebrateStyler functions
consoleStyler(\'#1d5c63\', \'#ede6db\', \'40px\', \'Congrats!\');
celebrateStyler(\'birthday\');
// Task 4: Insert a congratulatory and custom message
function styleAndCelebrate(color, background, fontSize, txt, reason) {
consoleStyler(color, background, fontSize, txt);
celebrateStyler(reason);
}
// Call styleAndCelebrate
styleAndCelebrate(\'ef7c8e\', \'fae8e0\', \'30px\', \'You made it\', \'champions\');
第二个函数(任务 2)应该接受一个单参数,原因,应该是字符串数据类型。如果我在 VSC 中运行代码,它就可以工作。但是,在评分系统中,我得到以下信息:
失败的测试 2:不记录celeryStyler() 变量
失败的测试 3:不调用 consoleStyler() 和celebrateStyler()
我尝试了不同的选项将参数声明为字符串(主要使用 typeof),但不幸的是我得到了相同的结果。
您能否看一下并就如何仅使用 JS 处理这种情况给我一些建议? 感谢您的时间!
-
celebrateStyler只接受单个参数,但是在任务 3 中,您正在使用 4 个参数执行该函数。 -
我认为这应该满足;
if(typeof reason !== \'string\') { //return some error } // otherwise run the function
标签: javascript string parameters functional-programming