A positive number is a very simple algorithm that is being asked sometimes in interviews to understand the basic logic of employees.
The question for an algorithm is,
Given an integer x, return positive/negative. The algorithm should return 'true' for positive numbers and "false" for negative numbers.
Example 1:
Input: x=123
Output: true
Example 2:
Input: x = -123
Output: false
Let's start with the algorithm,
var isPositive = function(x) {
if(x > 0){
return true;
}
return false;
};
Here, we have just checked that x is greater than zero(0) then the number becomes positive.
I hope you understood the algorithm for positive and negative, if you have any doubts please ket us know in the comments.
0 Comments