【问题标题】:How do I deal with SyntaxError: Unexpected token: Typo [closed]我该如何处理 SyntaxError: Unexpected token: Typo [关闭]
【发布时间】:2022-01-09 08:23:20
【问题描述】:

我想知道我的错误的逻辑以及我应该如何编写它,请。谢谢你

这是问题:

编写一个名为“findShortestOfThreeWords”的函数。 给定 3 个字符串,“findShortestOfThreeWords”返回给定字符串中最短的一个。 如果有关系,它应该返回参数列表中的第一个单词。

我得到的错误:

/home/runner/Module-1/index.js:21 }esle{ ^ SyntaxError:意外的令牌'{'

我的代码:

function findShortestOfThreeWords(word1, word2, word3){
  //if word1 was less than or equal word2
    //return word1
  //else
    //return word2
  //if word1 is less than or equal word3
    //return word1
  //esle 
    //return word3
  //if word2 is less than or equal word3
    //return word2
  //else
    //return word3

if(word1.length <= word2.length){
  return word1;
}else{
  return word2;
}if(word1.length <= word3.length){
  return word1;
}esle{
  return word3;
}if(word2.length <= word3.length ){
  return word2;
}else{
  return word3;
}
  
}

var output = findShortestOfThreeWords('a', 'two', 'three');
console.log(output); // --> 'a'

【问题讨论】:

  • 您是否尝试过改正错字?在您的代码中找到 esle

标签: javascript conditional-statements


【解决方案1】:

esle 更改为else

function findShortestOfThreeWords(word1, word2, word3){
  //if word1 was less than or equal word2
    //return word1
  //else
    //return word2
  //if word1 is less than or equal word3
    //return word1
  //esle 
    //return word3
  //if word2 is less than or equal word3
    //return word2
  //else
    //return word3

if(word1.length <= word2.length){
  return word1;
}else{
  return word2;
}if(word1.length <= word3.length){
  return word1;
}else{
  return word3;
}if(word2.length <= word3.length ){
  return word2;
}else{
  return word3;
}
  
}

var output = findShortestOfThreeWords('a', 'two', 'three');
console.log(output); // --> 'a'

【讨论】:

  • 请投票关闭作为一个错字
  • 谢谢。我很高兴这是一个错字。 :)
【解决方案2】:

您的代码中似乎有一个错字: esle{ 返回 word3;

尝试将其更改为 else

【讨论】:

  • 天啊 :)。谢谢
猜你喜欢
  • 2013-07-07
  • 2020-10-18
  • 2021-08-13
  • 1970-01-01
  • 2016-09-01
  • 2012-05-17
  • 2019-02-10
  • 2019-11-10
  • 2014-07-08
相关资源
最近更新 更多