skip to Main Content

I’m trying to solve this exercise:

Write a JavaScript code that asks the user for a number and then print the number of times that number appears in an array.

I’m doing it with parseFloat so I can get the string from the user with a <input /> and change it to number, then I declared my own array with numbers.

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> 

3

Answers


  1. Maybe try this

    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);
    }
    Login or Signup to reply.
  2. It looks like there are a few issues in your code. You’re trying to loop through the number, but it should be an array, not a string. You also have a typo in repeating and a logical issue in the comparison. Here’s a corrected version of your code:

    <!DOCTYPE html>
    <html>
    <head>
      <title>Number Repeater</title>
    </head>
    <body>
      <input type="text" id="number" placeholder="Enter a number">
      <button onclick="repeatNumber()">Check</button>
      <p id="compare"></p>
    
      <script>
        function repeatNumber() {
          const inputNumber = parseFloat(document.getElementById("number").value);
          const myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
          let count = 0;
    
          for (let i = 0; i < myArray.length; i++) {
            if (myArray[i] === inputNumber) {
              count++;
            }
          }
    
          const repeating = document.getElementById("compare");
          repeating.textContent = "Your number appears " + count + " times in the array.";
        }
      </script>
    </body>
    </html>
    

    In this corrected code:

    1. We fixed the loop to iterate through the myArray and compared each element with the inputNumber.
    2. We used a count variable to keep track of how many times the number appears in the array.
    3. We updated the repeating element’s text content to display the count.
    Login or Signup to reply.
  3. 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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search