0

Hi, I am wondering how I would be able to do this code. I have an idea on how to make the javascript code, but I'm not sure if my code is correct. The question I have been given is:

Design and develop a javascript program that will determine if input number is positive and negative.Consider zero(0) as positive(considering that it contains no negative sign).

here is my javascript code, I'm not sure if this is correct based on the question above.

<!DOCTYPE html>
<html>
<head>
<title>activity 1</title>
</head>
<body>
  <script type="text/javascript">
    var stringNumber = prompt("Enter Number: ", "");
    stringNumber = parseInt(stringNumber);
    if(stringNumber >=80){
      document.write("Positive");
    }else{
      document.write("Negative");
    }
  </script>
</body>
</html>

any answers are greatly appreciated.

4
  • 4
    Where does 80 come from? if(stringNumber >= 0) - you should also look at providing 10 as the second argument to parseInt(). Commented Jan 14, 2014 at 15:10
  • sorry just my idea on the 80 Commented Jan 14, 2014 at 15:13
  • 2
    The question is flawed. "Design and develop a javascript program that will determine if input number is positive and negative." No number can be both positive AND negative. Except sometimes for zero, but the question already says to consider that a positive. Commented Jan 14, 2014 at 15:14
  • ahh yes im just wondering when i input -1 it now goes to negative Commented Jan 14, 2014 at 15:22

4 Answers 4

1

Just check if the number is greater than or equal to 0

if(stringNumber >= 0)
{
    document.write("Positive");
}
else
{
    document.write("Negative");
}
Sign up to request clarification or add additional context in comments.

2 Comments

i have question is that ok i use parseInt(stringNumber) ?? when i remove the parseInt but still the same answer..
parseInt will convert the string input into an integer data type. Since the right hand side of this comparison is a number, javascript will convert the left hand side to one as well. So no, the parseInt is not necessary, but good practice anyway.
0
var theNumber = prompt("Enter a Number:");

if (theNumber < 0) {
   document.write("Negative");
}
else {
   document.write("Positive");
}

1 Comment

Things entered in the prompt are strings. You need to call parseInt or parseFloat to convert them to numbers.
0

Why you comparing number with 80? It should be 0

<script type="text/javascript">
    var stringNumber = prompt("Enter Number: ", "");
    stringNumber = parseInt(stringNumber);
    if(stringNumber >=0){
      document.write("Positive");
    }else{
      document.write("Negative");
    }
  </script>

And it work fine

1 Comment

yah sorry on that i was just thinking like when i input a lower number outputs negative
0

Positive, negative & NaN:

var stringNumber = prompt("Enter Number: ");

stringNumber = parseInt(stringNumber, 10); 

if (isNaN(stringNumber)) {
    document.write("Not a Number");
} else if(stringNumber >=0) {
    document.write("Positive");
} else {
    document.write("Negative");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.