【发布时间】:2022-10-24 14:50:14
【问题描述】:
我正在学习函数式编程的概念,遇到了一个让我很难过的练习。当我在任务 4 中调用 consoleStyler() 时,它给了我一个参考错误:颜色未定义。我按照课程中的分步说明获取此当前代码,并自己玩弄它,但我似乎无法满足错误。有人可以向我解释为什么我会收到这个错误吗?我真的很想了解我做错了什么。
// 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', '4px', 'Congrats!');
celebrateStyler('birthday');
// Task 4: Insert a congratulatory and custom message
function styleAndCelebrate() {
consoleStyler(color, background, fontSize, txt);
celebrateStyler(reason);
}
// Call styleAndCelebrate
styleAndCelebrate('ef7c8e', 'fae8e0', '30px', 'You made it!', 'Champions')
【问题讨论】:
-
在
styleAndCelebrate函数中,这些变量不存在。你的意思是输入你自己的输入吗? -
如果您在函数 3 中有函数 1 和函数 2,那么您需要将所有参数发送到函数 3。这样您就可以根据需要在函数 1 和 2 中使用这些参数
标签: javascript