Count instances of string in an array
Using a basic, old-fashioned loop:
var numOfTrue = 0;
for(var i=0;i<Answers.length;i++){
if(Answers[i] === "true")
numOfTrue++;
}
or, a reduce
var numOfTrue = Answers.reduce((acc,curr) => {
if(curr === "true")
acc++;
return acc;
},0);
or a filter
var numOfTrue = Answers.filter(x => x === "true").length;
You don't need jQuery for this.. a simple for loop like below would do the trick,
var numOfTrue = 0;
var Answers = [ "true", "false", "false", "true", "false" ];
for (var i = 0; i < Answers.length; i++) {
if (Answers[i] === "true") { //increment if true
numOfTrue++;
}
}
or even without a loop, DEMO
Answers.toString().match(/true/g).length