I am new to JS and am practicing writing logic. Any help/criticism is appreciated. For the entire function, not just my specific question.
My question is: why does this code return –> [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)
But 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 write:
return divisors if divisors != [] else f'{integer} is prime’
If you could explain exactly what is going on between the two code blocks that would be great
3
Answers
That’s the result of this operation:
If
divisors
is a truthy value then this expression resolves to the result ofdivisors != []
, which istrue
. Otherwise, it resolves to the string'test${interger} is prime'
.Presumably your intent here is to return
divisors
if it’s non-empty, and the string otherwise. In that case you could test the length of the array, and return the array itself (not a comparison with an empty array). For example:Additionally, if that string is meant to be a template literal then it’s using the wrong quotes:
As an aside… This is not a "single line if statement". This is an expression using the ternary conditional operator. While the functionality has similar aspects, a ternary conditional expression and an
if
statement/block are not drop-in replacements for one another.The conditional operator has three parts:
You wrote:
So the condition is
divisors
(i.e. Isdivisors
a true value).Then if it is, you return
divisors != []
. The!=
operator gives you a boolean. No value will be equal to a brand new array, sodivisors != []
will always be true.Then you have three problems
divisors
will never not be equal to a brand new array. The test for "Is it an empty array" is to test itslength
.Thus:
You can simplify
divisors.length > 0
todivisors.length
because0
is a false value and any other number is a true value.As per my understanding you’re attempting to find out the divisors for a given integer, in your case, 15.
The divisors of number 15 are [1, 3, 5, 15] and this, assumingly, should be your output.
That’s not the case as your starting your loop iteration at 2 (explicitly excluding divisor 1) and only going up till half the integer, which means you’ll end up excluding the integer itself as a divisor aswell.
This being the case, you’re left with 3 and 5, as your first result shows.
Regarding the second result:
Your one liner/single statement/ternary validates whether divisor has a value. If it has a value it returns the result to the condition:
Given that it has a value, [3, 5] as seen previously and the statement above is true, you’re getting True as your function output.
What I believe you want to achieve:
The ternary statement above evaluates the first condition (divisor different from empty array) and if it is true it returns divisors, otherwise it returns the second condition (string), in the following format:
Please let me know if the answer was clear enough and there’s anything else I can help with.