gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:31:10 25 4
gpt4 key购买 nike

我有一个 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)应该接受一个单一参数,原因,它应该是字符串数据类型。如果我在 VSC 中运行代码,它就可以工作。但是,在评分系统中,我得到以下信息:

Failed Test 2: Not logging celebrateStyler() variables

Failed Test 3: Not calling consoleStyler() and celebrateStyler()

我尝试了不同的选项来将参数声明为字符串(主要使用 typeof),但不幸的是我得到了相同的结果。

您能否看一下并就如何只使用 JS 来处理这种情况给我一些建议?感谢您的宝贵时间!

最佳答案

完整的解决方案得到 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

//

关于javascript - 如何在 JS 函数中声明参数仅为字符串数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73248652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com