Javascript 如何解决这个问题;如果;陈述

Javascript 如何解决这个问题;如果;陈述,javascript,Javascript,我真的不知道这个if语句的正确格式。我想让它计算我的txt文件中每个单词被使用的频率 function countWords(array, word, index) { var count = 0; var value = " " for (var i = 0; i < array.length; i++) { if (array[i] == 0 && value == word) count++; } } if (getUserS

我真的不知道这个if语句的正确格式。我想让它计算我的txt文件中每个单词被使用的频率

function countWords(array, word, index) {
  var count = 0;
  var value = " "
  for (var i = 0; i < array.length; i++) {
    if (array[i] == 0 && value == word)
      count++;
  }
}



if (getUserSelectionForm.problem.value == "pay") {
  countWords(working2DArray, "pay", 0)
  if (getUserSelectionForm.problem.value == "staffing") {
    countWords(working2DArray, "staffing", 0)
    if (getUserSelectionForm.problem.value == "hours") {
      countWords(working2DArray, "hours", 0)
      if (getUserSelectionForm.problem.value == "management") {
        countWords(working2DArray, "management", 0)
        console.log(countWords)
        document.getElementById('section2').innerHTML = "The selected word      appears " + countWords + " times in the array."
      }
函数countWords(数组、单词、索引){
var计数=0;
var value=“”
对于(var i=0;i
尽量不要使用多个
IF
语句,而是使用switch语句。这会使代码更加清晰

例如

因此:


尽量不要使用多个
IF
语句,而是使用switch语句。这会使代码更加清晰

例如

因此:


您在代码中犯了三个错误:

  • 您缺少一些if块的右大括号
    }
  • 您不能从函数中
    返回任何内容。您应该返回
    count

  • countWords
    是一个函数,您不需要显示它。您需要显示它的结果
您可以使代码更简单。您根本不需要这些
if
语句,因为您正在向函数传递与
getUserSelectionForm.problem.value
相同的值,所以直接传递它

function countWords(array, word, index) {
   var count = 0;
   var value= " "
   for(var i = 0; i < array.length; i++){
      if(array[i] == 0 && value == word)
         count++;
   }
   return count;
}
let word = getUserSelectionForm.problem.value
document.getElementById('section2').innerHTML = `The selected word appears ${countWords(working2DArray,word,0)} times in array` 

您在代码中犯了三个错误:

  • 您缺少一些if块的右大括号
    }
  • 您不能从函数中
    返回任何内容。您应该返回
    count

  • countWords
    是一个函数,您不需要显示它。您需要显示它的结果
您可以使代码更简单。您根本不需要这些
if
语句,因为您正在向函数传递与
getUserSelectionForm.problem.value
相同的值,所以直接传递它

function countWords(array, word, index) {
   var count = 0;
   var value= " "
   for(var i = 0; i < array.length; i++){
      if(array[i] == 0 && value == word)
         count++;
   }
   return count;
}
let word = getUserSelectionForm.problem.value
document.getElementById('section2').innerHTML = `The selected word appears ${countWords(working2DArray,word,0)} times in array` 

据我所知,您希望在出现问题时触发函数。value=='pay'| | | |'staffing'| | | |'hours'| |'management',以下是更清晰的版本供您参考:

var problemValue = getUserSelectionForm.problem.value;
var isProblemValueMatch = ["pay", "staffing" ,"hours", "management"].includes(problemValue);
if (isProblemValueMatch  ) {
  var countWords = working2DArray.filter(function(v) {
    return v === problemValue;
   }).length;
  console.log(countWords)
  document.getElementById('section2').innerHTML = "The selected word appears " + countWords + " times in the array."
}

据我所知,您希望在出现问题时触发函数。value=='pay'| | | |'staffing'| | | |'hours'| |'management',以下是更清晰的版本供您参考:

var problemValue = getUserSelectionForm.problem.value;
var isProblemValueMatch = ["pay", "staffing" ,"hours", "management"].includes(problemValue);
if (isProblemValueMatch  ) {
  var countWords = working2DArray.filter(function(v) {
    return v === problemValue;
   }).length;
  console.log(countWords)
  document.getElementById('section2').innerHTML = "The selected word appears " + countWords + " times in the array."
}

在编写代码时,您可能会考虑使用一致的缩进——这将使阅读和调试变得更容易,不仅对于潜在的回答者,而且对您来说,当我们都能看到<代码> {<代码> >代码>
块和它们的嵌套级别一目了然,而不必仔细地挑出每一行,只是为了在逻辑路径上挑出它们。
countWords
是一个函数,您可以在
innerHTML
中显示它……嗨!我使用javascript美化器对代码进行缩进编辑了您的代码。当代码缩进时,是不是更容易看到你已经错过了块的结束标记。这些答案有没有帮助回答你的问题?如果是这样,那么你应该接受一个。你可以考虑在编写代码时使用一致的缩进-它会使阅读和调试变得更容易,不仅对潜在的回答者,而且对你来说,当我们可以。都可以看到
{
}
块和它们的嵌套级别一目了然,而不必仔细地挑出每一行,只是为了在逻辑路径上挑出它们。
countWords
是一个函数,您可以在
innerHTML
中显示它……嗨!我使用javascript美化器对代码进行缩进编辑了您的代码。当代码缩进时,是不是很容易看出您错过了区块的结束标记。这些答案中有没有对回答您的问题有帮助?如果有,那么您应该接受一个。
[“支付”、“人员配置”…。我想includes(problemValue)
会更短。@MaheerAli您是对的,编辑过,谢谢您的建议。
[“支付”、“人员配置”…。includes(problemValue)
我想会短一些。@MaheerAli你说得对,编辑过,谢谢你的建议。我只想检查几个项目!从txt文件拉取时“let”仍然有效吗?我只想检查几个项目!从txt文件拉取时“let”仍然有效吗?
var problemValue = getUserSelectionForm.problem.value;
var isProblemValueMatch = ["pay", "staffing" ,"hours", "management"].includes(problemValue);
if (isProblemValueMatch  ) {
  var countWords = working2DArray.filter(function(v) {
    return v === problemValue;
   }).length;
  console.log(countWords)
  document.getElementById('section2').innerHTML = "The selected word appears " + countWords + " times in the array."
}