我很难回答问题,并希望得到任何帮助:
声明名为bowl set的数组等于以下内容:
["jelly-bean", "m&m", "m&m", "chocolate", "m&m", "jelly-bean",
"m&m", "m&m", "jelly-bean"]
接下来,声明三个变量:jbOccur
,mOccur
,chocOccur
。将每个设置为0。
jbOccur
变量在发生后应等于3
功能已经运行)。到目前为止,这是我能够生产的,所以你知道我在哪里......:
var bowl = ["jelly-bean", "m&m", "m&m", "chocolate", "m&m", "jelly-bean", "m&m", "m&m", "jelly-bean"]
var jbOccur = 0
var mOccur = 0
var chocOccur = 0
var occurence = function (jbOccur, mOccur, choOccur){
for (var i = 0; i < bowl.length; i++) {
console.log(bowl[jbOccur, mOccur, chocOccur])
}
};
答案 0 :(得分:-1)
试试这个:
var bowl = ["jelly-bean", "m&m", "m&m", "chocolate", "m&m", "jelly-bean", "m&m", "m&m", "jelly-bean"]
var stringOccur = function(stringArray, toFind){
var occurance = 0;
for (var elem in stringArray){
if (stringArray[elem].indexOf(toFind) != -1){
occurance++;
}
}
return occurance;
}
var jbOccur = stringOccur(bowl, "jelly-bean");
var mOccur = stringOccur(bowl, "m&m");
var chocOccur = stringOccur(bowl, "chocolate");
console.log(jbOccur,mOccur,chocOccur);