简体   繁体   English

Javascript:为什么我的代码什么都不提醒?

[英]Javascript: Why is my code alerting to nothing?

I've written the following: 我写了以下内容:

var pages=["[www.facebook.com] Facebook is cool. ","[www.bbc.co.uk] British broadcasting corporation. "];

function findScoreC2(s){ 
  var scores=[];
  var percentageScores=[];
  var contentsArray=[];
  s=s.toLowerCase();
  for(i=0;i<pages.length; i++){
    contentsArray=pages[i].substring(pages[i].indexOf("]")+1,pages[i].lastIndexOf(" "));
    var lowerCaseContents=(pages[i].substring(pages[i].indexOf("]")+1,pages[i].lastIndexOf(" "))).toLowerCase();
    scores[i] =(lowerCaseContents.split(s)).length-1
  };

  percentageScores=(scores[i] / contentsArray[i].length) * 100;
  var finalArray=[];

  for(i=0;i<percentageScores.length;i++){
    finalArray.push("{score:"+percentageScores[i]+",index:"+i+"}")
  };
  alert(finalArray);
}


findScoreC2("facebook");

however, alert(finalArray) alerts to nothing (ie an alert box comes up but it says nothing) when it should alert "{score:33,index:0},{score:0,index:1}" . 但是,当alert(finalArray)应该警告"{score:33,index:0},{score:0,index:1}"时,什么也不会警告(即出现警告框,但什么也没说)。

Could anyone enlighten me as to why this might be? 有人能启发我为什么会这样吗?

Thanks very much 非常感谢

You set percentageScores to a number. 您将percentageScores设置为一个数字。 You then try to iterate up to its length property, which gives you undefined when you do percentageScores.length , so the for loop never iterates. 然后,您尝试迭代到它的length属性,使您undefined当你这样做percentageScores.length ,所以for循环迭代永远。 You then alert with an empty array, whose toString produces the empty string. 然后,您使用一个空数组发出警报,该数组的toString生成空字符串。

You probably want this: 您可能想要这样:

for(i=0;i<pages.length; i++){
    contentsArray=pages[i].substring(pages[i].indexOf("]")+1,pages[i].lastIndexOf(" "));
    var lowerCaseContents=(pages[i].substring(pages[i].indexOf("]")+1,pages[i].lastIndexOf(" "))).toLowerCase();
    scores[i] =(lowerCaseContents.split(s)).length-1
    percentageScores[i]=(scores[i] / contentsArray[i].length) * 100;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript不是用C#背后的代码来警告数据库中的字符串变量值,而是警告整数变量,为什么? - Javascript not Alerting String Variable Value From Database in Code behind C#, but alerting integer variable, why? 为什么此代码不警告文本文件中的文本? (javascript / html) - Why is this code not alerting the text in the text file? (javascript/html) Javascript中的继承:为什么这段代码什么都不做? - Inheritance in Javascript: Why does this code do nothing? 为什么它没有从WeathermapAPI提醒我的信息 - why it's not alerting my information from WeathermapAPI 为什么我的程序提示未定义的值 - Why my program alerting undefined value 这是我的键码输入代码,但什么也没发生 - 普通 Javascript 而不是 jquery - This is my code for keycode inputs, but nothing is happening - plain Javascript and not jquery javascript类中未警告的jQuery最接近函数变为未定义的值 - Jquery closest function with class not alerting in javascript code become undefined value Javascript不会从C#代码中警报字符串变量值? - Javascript not Alerting String Variable Value From Code behing C#? JavaScript:提醒我的实时数据库中的特定值 - JavaScript: alerting a specific value on my real-time database 为什么我的 Puppeteer.innerText 代码什么也不返回? - Why Does My Puppeteer .innerText code return nothing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM