Looking to improve my JavaScript logic skills as a beginner. Appreciate any tips or feedback on the function and not just this specific question.
I'm curious why the code outputs --> [3,5]
function divisors(integer) {
let divisors = []
for (let i = 2; i < (integer/2); i++){
if (integer % i === 0){
divisors.push(i)
}
}
return divisors
};
divisors(15)
However, the following code returns True:
function divisors(integer) {
let divisors = []
for (let i = 2; i < (integer/2); i++){
if (integer % i === 0){
divisors.push(i)
}
}
return divisors ? divisors != [] : 'test${interger} is prime'
};
divisors(15)
This is what I am trying to achieve:
return divisors if divisors != [] else f'{integer} is prime'
If you could break down what's happening between these two sets of code, that would be really helpful!