I'm writing a function that should return Array when it's true and a string when false
I first wrote like this :
return (myArr != [])? myArr:`${integer} is prime`;
but when myArry is empty, istead of receiving ${integer} is prime I get an empty arr [],
When I write return (myArr.length != [])? myArr:`${integer} is prime, it works, and I dont understand why?
below my Code :
function divisors(integer) {
var i = 2;
var myArr = [];
do {
(integer % i) ? null : myArr.push(i);
i++
} while (i < integer);
return (myArr != []) ? myArr : `${integer} is prime`;
}
console.log(divisors(42));
myArr.length != []->myArr.length != 0myArr != []is always true, since an object is never equal to another object.do..whileor even the conditional expression. This seems like the job for a bog standard loopfor (let i = 2; i < integer; i++)and the body is a simpleif (integer % i === 0) myArr.push(i);