简体   繁体   中英

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')

I am getting an error that message is not defined. Now I know that var is function scoped so it can't be used outside the function. but Assignment on Coursera insists that

Yes, you can't use a variable outside of the scope it's defined.
However, the first error I get when trying to run your code is that color is undefined in the styleAndCelebrate function.

You didn't define the arguments for styleAndCelebrate so neither of the parameters were passed into the function in on your last line:

styleAndCelebrate('#ef7c8e', '#fae8e0', '30px', 'You made it!', 'champions')

To fix this, define the required parameters for styleAndCelebrate :
function styleAndCelebrate(color, background, fontSize, txt, reason)

You're going to have the same problem with celebrateStyler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM