Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Tuesday, February 21, 2023

JavaScript check if number is negative or positive

 JavaScript check if number is negative or positive

 



Introduction:


JavaScript is a versatile programming language that can be used to develop various web applications, including dynamic user interfaces and interactive websites. One common requirement when working with numbers in JavaScript is to determine whether a number is positive or negative. This can be done using a simple if statement, which allows you to check the sign of a number and take appropriate action based on the result. In this blog post, we will explore how javascript check if a number is negative or positive.


 
WAP to check whether the input number is positive, negative or zero.

JavaScript check if number is negative or positive


 
Explanation:
i) We use the prompt() function to ask the user to input a number. The prompt() function displays a dialog box with an input field for the user to enter a value.

ii) We store the input number in a variable called number.

iii) We use an if-else statement to check whether the number is positive, negative, or zero. If the number is greater than 0, we display a message in an alert box saying that the number is positive. If the number is less than 0, we display a message in an alert box saying that the number is negative. If the number is exactly 0, we display a message in an alert box saying that the number is zero.

iv) We use the alert() function to display the result in an alert box. The alert() function displays a dialog box with a message and an OK button. The message in this case is whether the input number is positive, negative, or zero, which is stored in a string concatenated with the input number using the + operator.
 

WAP to check whether the input number is positive, negative or neutral by using a switch statement.
 
 


 
In above program, we first prompt the user to enter a number using the prompt() function and convert it to an integer using the parseInt() function.

Next, we check if the number is positive, negative, or zero using a switch statement. We use the true expression in the switch statement to evaluate the cases based on Boolean values.

If the number is greater than 0, the first case will evaluate to true and we'll display an alert message saying that the number is positive. If the number is less than 0, the second case will evaluate to true and we'll display an alert message saying that the number is negative. If the number is equal to 0, the default case will evaluate to true and we'll display an alert message saying that the number is zero.
 
 
WAP to check whether the input number is positive, negative or neutral by using the function.
 


 Explanation:
i) We define a function called checkNumber() that takes one parameter: number. The function uses an if-else statement to check whether the number is positive, negative, or zero, and returns a message in the form of a string.

ii) We use the prompt() function to ask the user to input a number and store it in a variable called inputNumber using the let keyword.
iii) We call the checkNumber() function and pass the inputNumber variable as an argument.

iv) The checkNumber() function checks whether the number argument is positive, negative, or zero using an if-else statement and returns a message in the form of a string.

v) We store the result of the checkNumber() function in a variable called result.

vi) We use the alert() function to display the result in an alert box. The alert() function displays a dialog box with a message and an OK button. The message in this case is the result of the checkNumber() function, which is stored in the result variable.

Output:






FAQ:


Q: What is a negative number? 
Ans: A negative number is any number that is less than zero. For example, -5, -10, and -100 are all negative numbers.

Q: What is a positive number? 
Ans: A positive number is any number that is greater than zero. For example, 5, 10, and 100 are all positive numbers.

Q: Can a number be both negative and positive in JavaScript? 
Ans: No, a number can only have one sign in JavaScript. It's either positive, negative, or zero.


Conclusion:


Javascript check whether a number is negative or positive is a fundamental operation in programming. JavaScript provides a simple and easy-to-use way to check whether a number is negative or positive using an if statement. Understanding this basic concept is essential for anyone who wants to write effective code in JavaScript. By following the steps outlined in this blog post, you can easily check whether a number is negative or positive in your JavaScript code.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages