如何打印一个数字在数组中重复多少次?JS公司

How can i print how many times a number repeat inside an array? Js

提问人:DvK 提问时间:9/14/2023 最后编辑:DvK 更新时间:9/15/2023 访问量:55

问:

我正在尝试解决这个练习:

编写一个 JavaScript 代码,要求用户输入一个数字,然后打印该数字在数组中出现的次数。

我正在这样做,这样我就可以从用户那里获取字符串并将其更改为 number,然后我用 numbers 声明了自己的数组。parseFloat<input />

function repeatNumber() {
  const number = parseFloat(document.getElementById("number").value);
  const myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  let compare = false;
  let count = 0;
  for (let i = 0; i < number.length; i++) {
    if (myArray[i] === number) {
      compare = true;
      count++;
    }
  }

  //compare is my id in a p tag where i'll show the result
  const repeating = document.getElementById("compare");

  repating.textContent = "Is your number in my array ?" + compare;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="/controlFlow/script.js"></script>
    <title>Js</title>
</head>
<body>
    <label for="number">Pls place a number to see if it's in my array</label>
    <input type="text" id="number">
    <button onclick="repeatNumber()">Check</button> 
    <p id="compare">  </p>
  
</body>
</html> 

JavaScript 数组函数 for-loop dom

评论

3赞 trincot 9/14/2023
在您的代码中,没有尝试计算出现次数?
0赞 DvK 9/14/2023
我有一个 let count=0;然后,如果我将其作为count++;,则在里面。但没有用:/所以我删除了它
0赞 Ouroborus 9/14/2023
显而易见的(尽管不一定是“最佳”)方法是遍历数组,将每个元素与提供的值进行比较并计算匹配项。
3赞 trincot 9/14/2023
它应该有效。请将问题中的代码替换为尝试计数的版本。
0赞 Mark Schultheiss 9/14/2023
也许将 HTML 添加为功能片段

答:

0赞 Кристијан Давитковски 9/14/2023 #1

也许试试这个

function repeatNumber() {

  const number = parseFloat(document.getElementById("number").value);
  const myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  let compare = false;
  var count = 0;
  
  for (let i = 0; i < myArray.length; i++) {
    if (myArray[i] === number) {
      count++;
      compare = true;
    }
  }
  const repeating = document.getElementById("compare"); //compare is my id in a p tag where i'll show the result
  repating.textContent = "Is your number in my array ?" + compare;
  console.log(count);
}

评论

1赞 M0nst3R 9/14/2023
请格式化您的代码并修复代码片段,您将 JavaScript 代码共享为 HTML。
0赞 Mark Schultheiss 9/14/2023
在片段编辑器的左侧有一个漂亮的“整洁”按钮/链接,可帮助您一键快速格式化!
0赞 DvK 9/15/2023
感谢您的反馈,我很抱歉这是我的第一篇文章
0赞 Al Mubassir Muin 9/15/2023 #2

function repeatNumber() {
        const numbers = [2, 4, 6, 4, 8, 4, 10, 4];
            const userInput = document.getElementById("number").value;
            const numberToCount = parseInt(userInput);
            if (!isNaN(numberToCount)) {
                const count = numbers.reduce((accumulator, currentValue) => {
                    return currentValue === numberToCount ? accumulator + 1 : accumulator;
                }, 0);

                const resultElement = document.getElementById("compare");
                resultElement.textContent = `Is your number in my array ? ${count} `;
            } else {
                alert("Invalid input. Please enter a valid number.");
            }
        }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="/controlFlow/script.js"></script>
    <title>Js</title>
</head>
<body>
    <label for="number">Pls place a number to see if it's in my array</label>
    <input type="text" id="number">
    <button onclick="repeatNumber()">Check</button> 
    <p id="compare">  </p>
  
</body>
</html>

评论

0赞 DvK 9/15/2023
有趣的是,我不知道在这种情况下我可以用来比较,我有点新,你可以告诉哈哈。谢谢。!isNaN
0赞 Al Mubassir Muin 9/15/2023
实际上,isNaN() 函数用于检查给定值是否为非法数字。 在 HTML 文件输入类型中是文本,这就是我检查的原因