Keith Masoner
Keith Masoner

Reputation: 19

How to fix this "if" statement

I don't really know the correct format to this if statement. I want it to count the frequency each word in my txt file was used.

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."
      }

Upvotes: 0

Views: 78

Answers (3)

IlludiumPu36
IlludiumPu36

Reputation: 4304

Try not to use multiple IF statements and use a switch statement instead. Makes code much clearer and cleaner.

E.g.

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
} 

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

So:

var p = getUserSelectionForm.problem.value;

switch (p) {
    case 'pay':
        countWords(working2DArray, "pay", 0);
        break;
    case 'staffing':
        countWords(working2DArray, "staffing", 0);
}

Upvotes: 2

David Tse
David Tse

Reputation: 27

In my understanding , you want to trigger the function whenever problem.value ==='pay'||'staffing'||'hours'||'management' ,here is clearer version for your reference:

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."
}

Upvotes: 0

Maheer Ali
Maheer Ali

Reputation: 36594

You are making three mistakes in code:

  • You are missing some of the closing curly braces } of you if blocks.
  • You do not return anything from function. You should return count.
  • countWords is a function you don't need to display that. You need to display its result.

You can make your code much simpler. You don't need these if statements at all because you are passing the same value of getUserSelectionForm.problem.value to the function so directly pass it.

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 you want to only check for few items then use create a array of "pay","staffing"... and use includes() on it

let word = getUserSelectionForm.problem.value

if(["pay","staffing","hours","management"].includes(word)){     
    document.getElementById('section2').innerHTML = `The selected word appears ${countWords(working2DArray,word,0)} times in array` 
}

Upvotes: 0

Related Questions