慧女神
2016-04-25 13:59
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>第三章习题-4</title>
</head>
<body>
<script>
function chengfa(want){
var show = new Array(want);
alert(show.length);
/*for(var i=1;i<show.length+1;i++)
{
show[i-1]=want*i;
document.write(want+"*"+i+" = "+show[i-1]+"<br>");
}*/
}
var Num;
while((Num=prompt("请输入数字",12))!=-1)
{
while(isNaN(Num)==true)
{
alert("请重新输入!");
var Num=prompt("请输入有效数字",12);
}
if(Num==-1)
{
break;
}
chengfa(Num);
}
</script>
</body>
</html>
不知道你是怎么想的,这么混乱。首先,12不是显示的,这是prompt(参数1,参数2)--第二个参数就是框里默认的显示值,还有,一次不要太多显示框,不然你会疯,你这就是一个除了-1,就不会终止的死循环,你前面都判断了,不是-1就会一直循环,所以出来循环时,他得知必然就是-1,所以意义何在?
还有你定义的函数的参数就是数组的长度,所以你直接判断want这个参数是否传进去就可以了,我不知道你是否想要这样,你自己看看吧。有问题再问吧。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>window对象</title>
<script type="text/javascript">
function chengfa(want){
var show = new Array(want);
alert(want);
for(var i=1;i<show.length+1;i++)
{
show[i-1]=want*i;
document.write(want+"*"+i+" = "+show[i-1]+"<br>");
}
}
var Num;
while((Num=prompt("请输入数字"))==null)
{
var Num=prompt("请输入有效数字");
}
alert(Num);
chengfa(Num);
</script>
</head>
<body>
<form>
<input type="button" value="点击我,打开新窗口" />
</form>
</body>
</html>
我不知道你在做什么,所以我稍微修改了一下,这是我的代码;
如果你想要自定义增加数组的长度,直接用show.length=Num就可以了呀。
.length不仅是返回数组的长度,也可以设定数组的长度
var show = new Array(); show.length = Num; alert(show.length);
Detect languageAfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanLaoLatinLatvianLithuanianMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPersianPolishPortuguesePunjabiRomanianRussianSerbianSesothoSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshYiddishYorubaZulu | AfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanLaoLatinLatvianLithuanianMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPersianPolishPortuguesePunjabiRomanianRussianSerbianSesothoSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshYiddishYorubaZulu |
我在函数中添加了alert(show[0]);发现会弹出12,我感觉应该是新建数组的时候只是把这个值当成第一个索引的值,没有新建相应长度的数组,解决的办法没有想到,说一下我的理解
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题