Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有Switch语句的意外标识符_Javascript_Switch Statement_Identifier - Fatal编程技术网

Javascript 带有Switch语句的意外标识符

Javascript 带有Switch语句的意外标识符,javascript,switch-statement,identifier,Javascript,Switch Statement,Identifier,我是JS的新手,我有点不确定我是否正确使用了switch语句,或者是否有其他错误。我一直收到一个意外的标识符错误。谁能告诉我这里怎么了?事先非常感谢 function likes(names) { var outPut = ""; switch(names){ case (names.length === 1): outPut = names[0] = "likes this"; break; case (

我是JS的新手,我有点不确定我是否正确使用了switch语句,或者是否有其他错误。我一直收到一个意外的标识符错误。谁能告诉我这里怎么了?事先非常感谢

function likes(names) {
    var outPut = "";

    switch(names){
        case (names.length === 1):
          outPut = names[0] = "likes this";
          break;
        case (names.length === 2):
          outPut = names[0] + "and" + names[1] + "like this";
          break;
        case (names.length === 3):
          outPut = names[0] + " , " + names[1] + " and " + names[2] + " like this";
          break;
        case (names.length >= 4):
          outPut = names[0] + names[1] + " and " names.length - 2 + "like this";
          break;
        default:
          outPut = "no one likes this";
    }
  return outPut;
}
看起来您忘记了在“和”和names.length之间加一个“+”

outPut = names[0] = "likes this";

另外,我认为第二个“=”应该是加号。

您在
names.length>=4

最干净的方法是使用
if-else

var output = "no one likes this";

if (names.length === 1) {
  outPut = names[0] = "likes this";
} else if (names.length === 2) {
  outPut = names[0] + "and" + names[1] + "like this";
} else(names.length === 3):
  outPut = names[0] + " , " + names[1] + " and " + names[2] + " like this";
} else if (names.length >= 4):
  outPut = names[0] + names[1] + " and " + (names.length - 2) + "like this";
}
如果仍要使用
开关
,可以稍微修改代码

function likes(names) {
    var outPut = "";

    switch(names.length) {
        case 0:
            outPut = "no one likes this";
        case 1:
          outPut = names[0] = "likes this";
          break;
        case 2:
          outPut = names[0] + "and" + names[1] + "like this";
          break;
        case 3:
          outPut = names[0] + " , " + names[1] + " and " + names[2] + " like this";
          break;
        default:
          outPut = names[0] + names[1] + " and " +  (names.length - 2) + " like this";
    }
  return outPut;
}

下面是使用
开关执行此操作的正确方法:

函数类(名称){
var输出=”;
开关(名称.长度){
案例0:
outPut=“没有人喜欢这个”;
打破
案例1:
输出=名称[0]+“喜欢这样”;
打破
案例2:
输出=名称[0]+”和“+名称[1]+”如此”;
打破
案例3:
输出=名称[0]+“,“+名称[1]+”和“+名称[2]+”像这样”;
打破
违约:
输出=名称[0]+”、“+名称[1]+”和“+名称[names.length-2]+”像这样;
打破
}
返回输出;
}

log(比如([“爱丽丝”、“鲍勃”、“凯撒”)这不是进行切换的正常方式。为什么不切换(names.length){case1:…
?您忘记了在
和之间有一个
+
名称.长度
您要找的是
if
/
else if
/
else
,而不是
switch
语句。我想尝试学习如何使用switch语句,但不确定该语句采用的是哪种表达式。我现在理解了这一部分。另外,我没有意识到我的打字错误,因此感谢您的提示把他们赶出去,并向所有人请求迅速的帮助。
function likes(names) {
    var outPut = "";

    switch(names.length) {
        case 0:
            outPut = "no one likes this";
        case 1:
          outPut = names[0] = "likes this";
          break;
        case 2:
          outPut = names[0] + "and" + names[1] + "like this";
          break;
        case 3:
          outPut = names[0] + " , " + names[1] + " and " + names[2] + " like this";
          break;
        default:
          outPut = names[0] + names[1] + " and " +  (names.length - 2) + " like this";
    }
  return outPut;
}