[英]I am getting an error that message is not defined
function consoleStyler(color, background, fontSize, txt)
{
var message = "%c" + txt;
var style = `color: ${color}`;
style += `background:${background}`;
style += `fontSize:${fontSize}`;
console.log(style)
}
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)
}
}
consoleStyler('#1d5c63', '#ede6db', '40px', 'congrats!');
celebrateStyler('birthday')
function styleAndCelebrate()
{
consoleStyler(color, background, fontSize, txt);
celebrateStyler(reason);
}
styleAndCelebrate('#ef7c8e', '#fae8e0', '30px', 'You made it!', 'champions')
我收到消息未定义的错误。 现在我知道 var 的范围是 function,所以它不能在 function 之外使用。但是 Coursera 上的作业坚持认为
是的,您不能使用它定义的 scope 之外的变量。
但是,我在尝试运行您的代码时遇到的第一个错误是styleAndCelebrate
function 中未定义color
。
您没有为styleAndCelebrate
定义 arguments,因此这两个参数都没有传递到最后一行的 function 中:
styleAndCelebrate('#ef7c8e', '#fae8e0', '30px', 'You made it!', 'champions')
要解决此问题,请为styleAndCelebrate
定义所需的参数:
function styleAndCelebrate(color, background, fontSize, txt, reason)
你会遇到与celebrateStyler
相同的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.