1

I see other solutions to my question but none that help me.

I want to create a function to find if a number is positive/negative. The function should take an integer argument and return true if the integer is positive and false if it is negative.

Also, prompt the user again and again if anything other than a number is entered

Here's the code so far

When I enter a number, it keeps alerting me it is true or false but won't let me enter another. How do I control my loop so I can ask until -1 is entered? It is not giving me a chance to enter -1

function isPositive(num) {

    var result;

    if (num >= 0) {
        result = true;
    } else if (num < 0) {
        result = false;
    }
    return result;
}

var num;
num = parseInt(prompt("Enter a number"));
while (num != -1) {
    alert(isPositive(num));

    if (isNaN(num)) {
        alert("No number entered. Try again");
        num = parseInt(prompt("Enter a number"));
        isPositive(num);
        while (num != -1) {
            alert(isPositive(num));
        }
    }
}
Pizzaman
  • 425
  • 5
  • 11
  • 19
  • It tells me every number is true even when I enter -1 – Pizzaman Dec 04 '14 at 13:16
  • You may check this solution: http://stackoverflow.com/questions/7037669/how-to-check-the-value-given-is-a-positive-or-negative-integer?answertab=active#tab-top – Mahbub Nov 19 '16 at 05:40

4 Answers4

3

There's a few things wrong with your code, so here's a rewrite with comments:

function isPositive(num) {
  // if something is true return true; else return false is redundant.
  return num >= 0;
}

// when you want to keep doing something until a condition is met,
// particularly with user input, consider a while(true) loop:
var num;
while (true) {
  num = prompt("Enter a number");
  // check for null here
  if (num === null) {
    alert("No number entered. Try again.");
    continue; // return to the start of the loop
  }

  num = parseInt(num, 10); // second argument is NOT optional
  if (isNaN(num)) {
    alert("Invalid number entered. Try again.");
    continue;
  }

  // once we have a valid result...
  break;
}
// the loop will continue forever until the `break` is reached. Once here...
alert(isPositive(num));
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • I think he wants the alert inside the loop, so it will tell if each number he enters is positive. The loop ends when you enter -1. – Barmar Dec 04 '14 at 13:18
3
Math.sign(number)

which returns either a 1, -1 or 0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign

treeseal7
  • 739
  • 8
  • 22
2

The number 0 is neither positive, nor negative! :P

function isPositive(num)
{
    if(num < 0)
        return false;
    else
        return true;
}

Or a simple way,

function isPositive(num)
{
    return (num > 0);
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

You are testing if it isn't -1. Try this:

if(num < 0){
...IS NEGATIVE...
}else{
...IS POSITIVE...
}

This checks if it is less than or greater than 0.

Barmar
  • 741,623
  • 53
  • 500
  • 612
DripDrop
  • 994
  • 1
  • 9
  • 18
  • He's only testing `-1` in the loop condition, because that's how the user says that he's done. – Barmar Dec 04 '14 at 13:19