【问题标题】:How to declare the parameter in a JS function to be only string data type?如何将 JS 函数中的参数声明为仅字符串数据类型?
【发布时间】:2022-08-15 16:21:17
【问题描述】:

我有一个初学者的 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 中运行代码,它就可以工作。但是,在评分系统中,我得到以下信息:

失败的测试 2:不记录celeryStyler() 变量

失败的测试 3:不调用 consoleStyler() 和celebrateStyler()

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

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

  • celebrateStyler 只接受单个参数,但是在任务 3 中,您正在使用 4 个参数执行该函数。
  • 我认为这应该满足; if(typeof reason !== \'string\') { //return some error } // otherwise run the function

标签: javascript string parameters functional-programming


【解决方案1】:

参数类型

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 不是字符串,则不应按照规范执行此日志记录。因此,您需要进行一些类型检查,如果参数是字符串,则只有执行您的逻辑。我在下面的 sn-p 中做了一个示例实现,您当然可以根据自己的需要进行调整:

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

【讨论】:

    【解决方案2】:

    你找到解决方案了吗?

    【讨论】:

      猜你喜欢
      • 2019-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 2021-09-20
      • 1970-01-01
      相关资源
      最近更新 更多