JavaScript字符串计数循环的误解

时间:2019-02-05 18:13:41

标签: javascript arrays string loops variables

我在执行基本任务时遇到麻烦。我需要编写一个JavaScript程序,其中包含至少五个字符串的数组,循环遍历该数组,并为每个项目调用一个函数;此函数应检查字符串的长度:

  • 如果字符串少于四个字符,则打印短语“少于四个”
  • 如果等于四个字符,则打印“恰好四个”
  • 如果超过四个,则打印“多于四个”

我尝试了很多事情,但是感觉好像在找错地方了。我了解这是基本知识,但我似乎无法将其包裹住……

我现在的代码:

var colors = ["teal", "violet", "silver", "green", "red", "purple"];
var count;

for (count = 0; count < colors.length; count++) {
  console.log(colors[count]);
}

if (colors > 4) {
  console.log("greater than 4");
}

if (colors < 4) {
  console.log("less than 4");
}

if (colors = 4) {
  console.log("is equal to 4");
}

4 个答案:

答案 0 :(得分:1)

较新版本的JavaScript支持for..of语法

const colors =
  [ "teal", "violet", "silver", "green", "red", "purple" ]
  
for (const c of colors)
{ if (c.length > 4)
    console.log(c, "greater than 4")
  else if (c.length < 4)
    console.log(c, "less than 4")
  else
    console.log(c, "equal to 4")
}

// teal equal to 4
// violet greater than 4
// silver greater than 4
// green greater than 4
// red less than 4
// purple greater than 4

您应该使用函数将循环和长度检查的关注点分开-

const colors =
  [ "teal", "violet", "silver", "green", "red", "purple" ]
  
const checkLength = str =>
{ if (str.length > 4)
    return "greater than 4"
  else if (str.length < 4)
    return "less than 4"
  else
    return "equal to 4"
}

for (const c of colors)
  console.log(c, checkLength(c))

// teal equal to 4
// violet greater than 4
// silver greater than 4
// green greater than 4
// red less than 4
// purple greater than 4

JavaScript是multi-paradigm language,因此它支持以多种样式编写同一程序-

const colors =
  [ "teal", "violet", "silver", "green", "red", "purple" ]

const checkLength = str =>
{ if (str.length > 4)
    console.log(`${str} is greater than 4`)
  else if (str.length < 4)
    console.log(`${str} is less than 4`)
  else
    console.log(`${str} is equal to 4`)
}

colors.forEach(checkLength)

// teal equal to 4
// violet greater than 4
// silver greater than 4
// green greater than 4
// red less than 4
// purple greater than 4

JavaScript对表达式的支持也非常好,无需使用命令式关键字,例如ifelseswitchforwhiledo甚至return-

const colors =
  [ "teal", "violet", "silver", "green", "red", "purple" ]

const checkLength = x =>
  x.length > 4                   // if ...
    ? `${x} is greater than 4`
: x.length < 4                   // else if ...
    ? `${x} is less than 4`
: `${x} is equal to 4`           // else

console.log(colors.map(checkLength))

// [ "teal is equal to 4"
// , "violet is greater than 4"
// , "silver is greater than 4"
// , "green is greater than 4"
// , "red is less than 4"
// , "purple is greater than 4"
// ]

答案 1 :(得分:1)

数组具有用于循环的内置方法,这些方法允许在每次循环迭代时执行回调函数。在您的情况下,由于只需要检查字符串,因此.forEach()方法可能是最合适的。

在该函数中,只需一个简单的if/then/else语句即可确定要打印的消息。

var colors = ["teal", "violet", "silver", "green", "red", "purple"];

colors.forEach(function(color){
  if(color.length < 4){
    console.log(color + " has less than 4 characters.");
  } else if (color.length === 4) {
    console.log(color + " has 4 characters.");  
  } else {
    console.log(color + " has more than 4 characters.");  
  } 
});

答案 2 :(得分:0)

在每个元素上调用一个函数,并检查if-else块内的长度

{{1}}

答案 3 :(得分:0)

您需要将if语句放在for循环的花括号内,因此对于每种颜色,它将通过所有if条件运行,并在匹配时打印。

一种更惯用的方式来执行您当前要执行的操作是在forEach函数的主体内实现逻辑,该主体是Array object's prototype

的一部分
var colors = ["teal", "violet", "silver", "green", "red", "purple"];

colors.forEach(function(currentColorToCheck) { //currentColorToCheck is a temporary variable that the forEach function gives you (1 for every item of colors Array)
  if (currentColorToCheck.length > 4) { // we use the .length function (part of String prototype and Array prototype) to get the length of the string
    console.log("greater than 4");
  }

  if (currentColorToCheck.length < 4) {
    console.log("less than 4");
  }

  if (currentColorToCheck.length === 4) { // here for an equality comparison, use === instead of =
    console.log("is equal to 4");
  }
})

forEach是表达“迭代数组的值”的更方便的方法。您可以查看文档以获取更多指导。

作为旁注,您可能想在业余时间研究每种JavaScript类型(对象,数组,字符串,数字,日期,数学等)的大量原型(内置)函数。 Mozilla Developer Network为此拥有大量资源。