微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我在让我的子字符串显示某个结果时磕磕绊绊

如何解决我在让我的子字符串显示某个结果时磕磕绊绊

我偶然发现了有关 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?