Dr.*_*nni -1 javascript if-statement
当且仅当两个args是数字(因此我的第一个函数)时,我想总结我的函数的args.
function checkNum() {
var num = 0;
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] !== 'number') {
return false;
}
}
return true;
}
function addTogether() {
var num = 100;
if ( checkNum() ) {
return arguments[0] + arguments[1];
} else {
return undefined;
}
}
addTogether(2, "");
Run Code Online (Sandbox Code Playgroud)
然而,无论args值是什么,我的第二个函数都会执行求和.关于如何解决这个问题的任何提示?
checkNum()没有被声明为显式地接受任何参数(这意味着任何人都在看这个函数时没有预期),并且当你调用它时你没有发送任何参数,所以arguments.length总是0,你永远不会进入你的循环体并且你总是返回true.
你的第二个功能是传递两个参数调用,所以你引用arguments[0]和arguments[1]有效存在.但是,即便如此,使用arguments并不是真正意义上的所有论证传递.
最好使用命名参数设置函数,然后可以通过这些名称访问它们.arguments不鼓励使用(虽然有效)作为访问参数的默认机制.它通常用于验证(例如,确保在函数尝试对它们进行操作之前将正确数量的参数传递给函数).
此外,最好用正则表达式测试数字,因为它typeof可以"撒谎"给你.例如:
// Would you ever think that not a number is of type "number"?!
console.log(typeof NaN === "number");Run Code Online (Sandbox Code Playgroud)
现在,根据您的"数字"标准,您可以采用两种方式.
// It's better for this function to test one number
// at a time, so you can react to that particular
// success or failure
function checkNum(num) {
// No loop and no if/then needed, just return
// whether the argument is a number, but don't
// test for typeof number because typeof NaN === "number"
// Use a regular expression instead
var reg = /[0-9]+$/; // digits or strings of characters that are from 0 - 9
// Test for only digits not numbers passed as strings
// For example 6 is good, "6" is bad. Here, the use of "typeof"
// is safe because you are also testing that the input is digits
// or characters from 0 to 9 (NaN wouldn't pass this test)
return reg.test(num) && typeof num === "number"; // true or false will be returned
}
function addTogether(val1, val2) {
// Test each input, independantly so that you can react more granularly
if ( checkNum(val1) && checkNum(val2) ) {
return val1 + val2;
}
// It's not necessary to have an "else" that returns undefined because
// that's what will happen as long as you don't return anything else.
}
console.log(addTogether(2, "")); // undefined
console.log(addTogether(2, 6)); // 8
console.log(addTogether(2, "6")); // undefined because "6" is a string, not a digitRun Code Online (Sandbox Code Playgroud)
// It's better for this function to test one number
// at a time, so you can react to that particular
// success or failure
function checkNum(num) {
// No loop and no if/then needed, just return
// whether the argument is a number, but don't
// test for typeof number because typeof NaN === "number"
// Use a regular expression instead
var reg = /[0-9]+$/; // digits or strings that are from 0 - 9
// Test for only digits and numbers passed as strings
return reg.test(num); // true or false will be returned
}
function addTogether(val1, val2) {
if ( checkNum(val1) && checkNum(val2) ) {
// If checkNum returns true for numeric characters as well as digits, then
// you'd need to ensure that the characters get converted to numbers so that
// you get mathmatical addition and not string concatenation. That would be done like this:
return +val1 + +val2
}
// It's not necessary to have an "else" that returns undefined because
// that's what will happen as long as you don't return anything else.
}
console.log(addTogether(2, "")); // undefined
console.log(addTogether(2, 6)); // 8
console.log(addTogether(2, "6")); // 8 because "6" is converted to 6, not a string of "6"Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
114 次 |
| 最近记录: |