skip to Main Content

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


  1. But the following code returns True

    That’s the result of this operation:

    divisors ? divisors != [] : 'test${interger} is prime'
    

    If divisors is a truthy value then this expression resolves to the result of divisors != [], which is true. 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:

    divisors.length > 0 ? divisors : 'test${interger} is prime'
    

    Additionally, if that string is meant to be a template literal then it’s using the wrong quotes:

    divisors.length > 0 ? divisors : `test${interger} is prime`
    

    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.

    Login or Signup to reply.
  2. The conditional operator has three parts:

    condition ? true value : false value
    

    You wrote:

    divisors ? divisors != [] : 'test${interger} is prime'
    

    So the condition is divisors (i.e. Is divisors 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, so divisors != [] will always be true.


    This is what I am trying to write: return divisors if divisors != [] else f'{integer} is prime’

    Then you have three problems

    • The condition goes first
    • As mentioned, divisors will never not be equal to a brand new array. The test for "Is it an empty array" is to test its length.
    • Template strings are marked with ` characters, not ‘ characters

    Thus:

    divisors.length > 0 ? divisors : `test${interger} is prime`
    

    You can simplify divisors.length > 0 to divisors.length because 0 is a false value and any other number is a true value.

    Login or Signup to reply.
  3. 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:

    divisors != []
    

    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:

    return divisors != [] ? divisors : f'{integer} is prime'
    

    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:

    return {condition} ? {value_if_true} : {value_if_false}
    

    Please let me know if the answer was clear enough and there’s anything else I can help with.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search