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

标签 javascript string parameters functional-programming

我有一个 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/

相关文章:

javascript - HTML5 Canvas 显示和拖动图像

javascript - 字符转义: from Python string literal to JSON and then to HTML

c - 将整数字符串存储到数组中

arrays - 在VB6中将数组作为参数传递

ruby - 为什么 Ruby 输出 [[ :rest]] when the parameter method is called on String objects?

php - 带有动态参数的 call_user_func

javascript - *ngFor 需要为每个不同的对象生成 1 个 btn,但将相似的对象值放在同一个 btn 中

javascript - 在 webdriver.io 中的 while 循环中链接 Promise

c# - 如何在 .NET C# 中解析阿拉伯文本字符串的每个字母?

python - 一个线性函数,用于根据 python 中的不同条件按升序、降序对列表的字符串列表进行排序