-1
我試圖擰一個腳本,輸出一個數組並根據用戶輸入計算數組中元素的數量。JavaScript計數函數
<script>
// Function searches a given array and counts the number of times an element appears in the array.
function countOccurences(numArray, num) // function
{ //parameters
var count = 0;
if(array[i] == 2)
count++;
}
</script>
</head>
<body>
<script>
var num = [2, 6, 33, 1, 77, 2, 98]; <!-- array -->
document.write("<p>"+nums.toString()+"</p>"); <!-- fill the array-->
var count = parseFloat(prompt("Please enter number to search for", "")); <!--ask for the numer to search -->
// linear search
var p = countOccurences(nums, key) ;
document.write("<p>Value "+p+" was not found in array </p>"); //output the count
</script>
</body>
</html>
這不是Java(而且你總是忘記語言標記)。你也沒有問過一個實際的問題或解釋這個代碼有什麼問題 – UnholySheep
這是什麼問題?附: '' - JavaScript的錯誤評論 – FieryCat