I can’t solve a javascript exercise

Asked

Viewed 2,043 times

-3

I’m doing an exercise and I can’t solve it:

The instructions are:

Write the eNumerDaSorte function which, receiving a number, tells you if it is a lucky number. Remember that the number must obey the three conditions mentioned. Your additional challenge will be: DO NOT use the if.

We can say it’s a lucky number if the number:

is positive

is a multiple of 2 or 3

is not 15

My answer was:


function eNumeroDaSorte (numero) {

  return numero > 0 && 15%3==0 || 10%2==0 && numero != 15; 

}

But the site is wrong with the following problems:

eNumerDaSorte is false and multiples of 2 or 3

eNumerDaSorte(7) is false

eNumerDaSorte(15) is false

Thanks for any help!

  • Good morning, although you have given us enough information to give us an answer, at least put a title to your question that relates to your problem. And yes, thinking about WHAT is the title of your question will make you a better programmer because it will take you to the analysis process.

  • Okay, I appreciate the comment and I’ll try to improve on the next.

3 answers

2


Well, I changed the code a little but I think eNumeroDaSorte(15) must be false, according to what you described, for it is not 15, even if it is multiple of 3, and eNumeroDaSorte(7) is also false because it is not multiple of 2 or 3.

function eNumeroDaSorte (numero) {
  return (numero >= 0) && ((numero % 3 === 0) || (numero % 2 === 0)) && (numero !== 15);
}

// é positivo
// é um múltiplo de 2 ou 3
// não é 15

console.log(eNumeroDaSorte(15))

I hope I’ve helped.

  • 1

    Hi, it helped yes, thank you very much. I think the prolema was in the part of %, that I had put a 15%3 and 10%2. I thank you for the help!

1

My answer is:

function eNumeroDaSorte (numero) {
  return (numero > 0) && ((numero % 3 == 0) || (numero % 2 == 0)) && (numero !== 15);
}

number can’t be >= 0 because 0 is not a multiple of 2 and 3 I hope I’ve helped.

-1

Function eNumerDaSorte (number){ Return(number > 0) && ((number % 4 === 0) || (number % 2 === 0)) && (number !== 15); } console.log(eNumeroDaSorte(15))

//I did so and it worked! I hope I helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.