var NumberArray = [5, 65, 02, 04, 05, 80, 90, 01, 46, 87, 05, 6, 20, 48, 056, 78, 9, 7, 88, 9, 87, 65, 48, 6, 54, 3, 51, 90, 95, 60, 100];
document.write("These are all of the numbers in the array");
document.write("<br>");
document.write(NumberArray);
document.write("<hr>");
var sum = 0;
for (var i = 0; i < NumberArray.length; i++) {
sum += Number(NumberArray[i]);
var average = sum/NumberArray.length;
}
document.write("This is the sum of the array");
document.write("<br>");
document.write(sum);
document.write("<hr>");
document.write("This is the average of the array");
document.write("<br>");
document.write(average);
function findNumber(Query) {
for (i = 0; i < NumberArray.length; i++) {
if (Query == NumberArray[i]) {
alert(Query + " was found at position " + i);
return;
}
}
alert(Query + "is not found in the array");
}
/* Not relevant here
var lookingFor = prompt("who to look for?");
for (i = 0; i < people.length; i++) {
if (lookingFor == people[i]) {
alert(lookingFor + " was found at position " + i);
}
}
*/
<title>
Array Search Test
</title>
<body>
<input type="text" id="Search" />
<button onclick="findNumber(document.getElementById('Search').value);">Search Array</button>
<hr />
<script src="external.js"></script>
</body>
소문자 '기능'findNumber, N은 죄송합니다, 여기에 업데이트 된 바이올린을 http – danronmoon
'findumber'에서 누락 된 자본 :! //jsfiddle.net/83r24j3j/5/ –
다시'document.write' 용서할 수없는 것. 그 습관을 깬다! 초보자에게는 jQuery 기본 사항을 배우고'.html '을 사용하는 것이 쉬운 습관을 버리는 것이라고 생각합니다. –