簡體   English   中英

我收到消息未定義的錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM