javascript 如何声明JS函数中的参数为字符串数据类型?

vddsk6oq  于 2023-01-16  发布在  Java
关注(0)|答案(4)|浏览(114)

我有一个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)应该接受单个参数reason,它应该是**string数据类型。**如果我在VSC中运行代码,它可以工作。但是,在评分系统中,我得到以下结果:
未通过测试2:不记录celebrateStyler()变量
未通过测试3:不调用consoleStyler()和celebrateStyler()
我尝试了不同的选项来将参数声明为字符串(大多数情况下使用typeof),但不幸的是,我得到了相同的结果。
你能不能看一看,并给予我一些建议,如何处理的情况下只使用JS?谢谢你的时间!

fhg3lkii

fhg3lkii1#

完全溶液得到100/100

// 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(reason,fontStyle);
    }
}

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

enter code here

//
agxfikkp

agxfikkp2#

参数类型

javascript是一种弱类型语言,在您的情况下,这意味着您不能强制某个类型为参数的类型。

理解错误

您将收到一个包含"测试失败:".
所以,非常有趣的部分是测试名称,它暗示了目标。

不记录celebrateStyler()变量

本节标题中的期望表明,有一个测试场景期望你的代码避免记录变量,让我们看看这个函数:

// 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); 
    }
}

无论reason是什么,这个函数都会对console.log执行一次调用。然而,如果reason不是字符串,你就不应该按照规范进行日志记录。所以,你需要做一些类型检查,如果参数是字符串,那么就执行你的逻辑。我在下面的代码片段中做了一个示例实现,当然你可以根据需要进行调整:

// Task 2: Build another console log message generator
function celebrateStyler(reason) {
    if (typeof reason === "string") {
        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(reason, fontStyle); 
        }
    } else {
        //If you want to do something with a nonstring reason, this is the right place to do it
    }
}

celebrateStyler('birthday'); //Happy Birthday
celebrateStyler('champions'); //Congrats on the title!
celebrateStyler('Welcome Back!'); //Welcome Back
celebrateStyler(42); //No logging, because it's not a string

注意,我已经将您上一个else块中的connsole.log更改为使用相同的样式,如果您不喜欢这样,当然可以使用您自己的消息和样式。

不调用consoleStyler()和celebrateStyler()

这是在告诉你styleAndCelebrate不应该调用上面提到的函数,因此,你需要检查reason的类型,如果不是字符串,就不要调用这些方法:

// Task 4: Insert a congratulatory and custom message
function styleAndCelebrate(color, background, fontSize, txt, reason) {
    if (typeof reason === "string") {
        consoleStyler(color, background, fontSize, txt);  
        celebrateStyler(reason);
    }
}
hfyxw5xn

hfyxw5xn3#

您可以在这里找到完整的解决方案。

Task 1: Build a function-based console log message generator
function consoleStyler(color,background,fontSize,txt) {
    message = "%c" + txt
    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) {
 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() {
    consoleStyler('ef7c8e','fae8e0', '30px', 'You made it!');  
    celebrateStyler('birthday');
}
// Call styleAndCelebrate
styleAndCelebrate();

希望这能像预期的那样工作!编码快乐...

ttygqcqt

ttygqcqt4#

// 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(reason, fontStyle);
    }
    
}

// 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');`enter code here`

相关问题