Maggie S
Maggie S

Reputation: 1

Printing prime numbers - console.log loop result to html

My goal was to print out prime numbers (not including the number inputted) below the original value of the input. When I wrote the function, I am able to see the results I need with console.log(i) but when I change it to return i, I don't get the results when I call the function showPrime(n). I think the issue is how I'm returning the results in showPrime(), would I need to create an empty string?

function isPrime(n) {
  //2 is an exception
  if ((n <= 1) || n % 2 == 0) {
    return false;
  } else {
    return true;
  };
};

function showPrime(n) {
  let numbies = []
  for (let i = 0; i < n; i++) {
    if (isPrime(i) === true) {
      console.log(i)
    }
  }
}

//user interface logic
$(document).ready(function() {
  $("form").submit(function(event) {
    event.preventDefault();
    n = $("input#numberInput").val();
    alert(showPrime(n));
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
  <h1>Prime Number Sifter</h1>
  <p>When you input a number, the program will return all prime numbers below it (not including the number). <em>Note: While 2 is a prime number, the program won't return anything since 1 is not!</em></p>
  <form>
    <div class="form-group">
      <label for="numberInput">Input </label>
      <input type="integer" class="form-control" id="numberInput" placeholder="Enter number">
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
  </form>
  <br>
  <div id="output">
    <p><span class="show"></span></p>
  </div>

Upvotes: 0

Views: 283

Answers (2)

Lukas
Lukas

Reputation: 36

a return statement interrupts and ends the function. if you put return i into the for loop (if I understood correctly) the function ends and returns 0.

Upvotes: 0

DDomen
DDomen

Reputation: 1878

If you use return it will stop the function execution, so you will stop just after the first number (pratically only 0).

You should take an array of numbers and return it

function isPrime(n) {
  //2 is an exception
  if ((n <= 1) || n % 2 == 0) {
    return false;
  } else {
    return true;
  };
};

function showPrime(n) {
  let numbies = []
  for (let i = 0; i < n; i++) {
    if (isPrime(i) === true) {
      // console.log(i)
      numbies.push(i)
    }
  }  
  return numbies;
}


//user interface logic
$(document).ready(function() {
  $("form").submit(function(event) {
    event.preventDefault();
    n = $("input#numberInput").val();
    alert(showPrime(n));
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
  <h1>Prime Number Sifter</h1>
  <p>When you input a number, the program will return all prime numbers below it (not including the number). <em>Note: While 2 is a prime number, the program won't return anything since 1 is not!</em></p>
  <form>
    <div class="form-group">
      <label for="numberInput">Input </label>
      <input type="integer" class="form-control" id="numberInput" placeholder="Enter number">
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
  </form>
  <br>
  <div id="output">
    <p><span class="show"></span></p>
  </div>

If you want just the highest prime number, make the inverse for loop.

function showPrime(n) {
  let numbies = []
  for (let i = n; i >= 0; i--) {
    if (isPrime(i) === true) {
      // console.log(i)
      return i;
    }
  }  
  return 0;
}

Upvotes: 1

Related Questions