find the deivisors

title

Create a function named divisors that takes an integer and returns an array with all of the integer’s divisors(except for 1 and the number itself). If the number is prime return the string ‘(integer) is prime’ (use Either String a in Haskell).

Example

1
2
3
divisors(12); //should return [2,3,4,6]
divisors(25); //should return [5]
divisors(13); //should return "13 is prime"

My solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function divisors(integer) {
var helf = Math.round(integer/2);
var arr = [];
for(var i = 2;i<=helf;i++){
if(integer%i == 0){
arr.push(i)
}
}
if(arr.length>0){
return arr;
}else{
return integer+" is prime";
}
};

good solution

1
2
3
4
5
function divisors(integer) {
var res = []
for (var i = 2; i <= Math.floor(integer / 2); ++i) if (integer % i == 0) res.push(i);
return res.length ? res : integer + ' is prime'
};