如何解决我在让我的子字符串显示某个结果时磕磕绊绊
我偶然发现了有关 typeofs 和 if/else 语句的代码
我对让我的子字符串显示某个结果感到困惑。它正在返回字符串而不是另一个字符串(“非字符串”):
/*
Follow the instructions - Create a function called "isstring" that takes 3 arguments (x1,x2,x3)
- check if each argument is a string using typeof.
- If each argument is a string,return "strings"
- If each argument is NOT a string,return "not strings"
*/
//Write your code here
////////////////////////////////////////
function isstring(a,b,c) {
if (typeof isstring === 'a','b','c') {
return "strings";
}
else if (typeof isstring !== 'a','c') {
return "not strings";
}
isstring(x1,x3);
}
////////////////////////////////////////
//open the browser console to check results
console.log('results: ',isstring('a','c'));
//don't change this line
if (typeof module !== 'undefined') {
module.exports = isstring;
}
解决方法
这样做:
function isString(a,b,c) {
const allStrings = [...arguments].every(a => typeof a === 'string');
return allStrings ? 'all strings' : 'not all strings';
}
console.log(isString('a','b','c'))
console.log(isString('a',8))
,
function isString(a,c) {
if (typeof a === 'string' && typeof b === 'string' && typeof c === 'string') {
return 'strings'
} else {
return 'not strings';
}
}
这是他们为我纠正的^
,遍历参数并检查类型:
function isString(a,c) {
var valid = true;
[...arguments].forEach(e => valid = typeof e != "string" ? false : true);
return valid ? "strings" : "not strings";
}
console.log(isString('a','c'))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。