题解 | #计数#
计数
https://www.nowcoder.com/practice/628339bd8e6e440590ad86caa7ac6849
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
/* 填写样式 */
</style>
</head>
<body>
<!-- 填写标签 -->
<script type="text/javascript">
//统计数组 arr 中值等于 item 的元素出现的次数
//1.定义函数,确定函数传参,1-数组 2.需要查询的值
function count(arr, item) {
console.log(arr, item)
//定义初始值,用于累计数组元素等于规定值相等时的次数
let count = 0
for(let i=0;i<arr.length;i++){
//循环数组,当数组元素等于规定值item时,累加count值
if(arr[i] ===item ){
count ++
}
}
//循环结束,抛出count值
return count
}
let arr = [1, 2, 4, 4, 3, 4, 3]
let item = 4
count(arr,item)
</script>
</body>
</html>
