There is a function signFunc(x)
that returns:
1
ifx
is positive.-1
ifx
is negative.0
ifx
is equal to0
.
You are given an integer array nums
. Let product
be the product of all values in the array nums
.
Return signFunc(product)
.
Example
Input: nums = [-1,-2,-3,-4,3,2,1]
Output: 1
Explanation: The product of all values in the array is 144, and signFunc(144) = 1
Solution
/**
* @param {number[]} nums
* @return {number}
*/
var arraySign = function (nums) {
//Set product to 1
let product = 1;
//FOR loop to iterate through nums array
for (let i = 0; i < nums.length; i++) {
//Set product to product * nums[i]
product = product * nums[i];
}
//IF product is greater than 0
if (product > 0) {
//Return 1
return 1;
//ELSE IF product is less than 0
} else if (product < 0) {
//Return -1
return -1;
//ELSE
} else {
//Return 0
return 0;
}
};