Help with conditionals with bodmas

hi

this is the question: Write a function that takes 2 numbers as input. If either of the numbers is 3 AND the sum of the numbers contains a 3 then return true. Otherwise return false

this is my code:

function numbersConditions(num1, num2) {
    var sumOfNums = num1 + num2
    var containsThree = sumOfNums.toString().indexOf("3") > -1; //returns true if 3 is in the sum
    if(num1 === 3 || num2 === 3 || containsThree === true) {
        return true
    } else {
        return false
    }
}

console.log(numbersConditions(7, 6))
console.log(numbersConditions(3, 6))
console.log(numbersConditions(7, 2))

please assist, I cannot get it to output true with “&&” operator, only the “||” operator works to get correct output on first function call

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

That is because if either of the numbers is 3 then it will return true.
But you are not satisfying the other part of the instructions where it has to be true when the sum of the numbers contains a 3.

You need to test with different numbers in these function calls here

Even with the correct code, all of those function calls are going to be false.

You need to test with two numbers where it can actually be true.

Test with this function call instead

console.log(numbersConditions(3, 10));

You could try using a nested if statement.

function numbersConditions(num1, num2) {
  var sumOfNums = num1 + num2;
  var containsThree = sumOfNums.toString().indexOf("3") > -1; //returns true if 3 is in the sum
  if (num1 === 3 || num2 === 3) {
    if(containsThree) {
      return true
    } else {
      return false
    }
  } 
}

console.log(numbersConditions(3, 10));
console.log(numbersConditions(3, 6));

the nested also works when i use a 3 as input but how can I change it that any 2 numbers as input (7, 6 which sum is 13) in my case also print true with the “&&” operator.
The reviewer said I should check on bodmas

Maybe I am confused on the instructions but how would this be true?

Your instructions say,

The number 7 or 6 does not equal three.

If you want it to return true wouldn’t one of the numbers in the function calls have to be 3?

the way i see it is 7+6 = 13 which contains a 3… maybe you are right in this case? so I wanted that to also output true

Maybe you can double check with your instructor about the instructions because I guess I understood it differently. :grinning:

If you wanted to use the && operator you can wrap those|| statements in parenthesis

function numbersConditions(num1, num2) {
  var sumOfNums = num1 + num2;
  var containsThree = sumOfNums.toString().indexOf("3") > -1; //returns true if 3 is in the sum
  if ((num1 === 3 || num2 === 3) && containsThree) {
    return true;
  } else {
    return false;
  }
}

console.log(numbersConditions(3, 10));
console.log(numbersConditions(3, 6));

Yes u were correct in your logic. Another user on reddit gave me the correct syntax to use. Thanks. I got it correct as I saw now in feedback

1 Like

Piece of cake… once you learn how to chain these common methods :slight_smile:

Mod Edit: SOLUTION REDACTED

This function will return a true if num1 or num2 are 3, AND if the number 3 appears anywhere in the sum of num1 and num2. So I add num1 and num2, convert it into a string with the toString() method, then use the split() method on the resulting string to create an array of string digits, then use the includes() method to see if there’s a “3” in there.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

1 Like

Hello Jeremy. I’m so sorry to have made such a faux-pas one of my first attempts at using the forum. Seeing the question in the weekly email I hopped onto my JavaScript playground and successfully solved the problem first try! That never happens, so I got a wee bit too zealous and posted my one-line answer, seeing how others posted their solutions with many more lines. I should have read the etiquette for posting before actually posting.

Moderator edit: I removed your email signature. The links in it may be considered self-promotion. When using your email to post on the forum please do not include the signature. Thank you.

Oh you’re good.

I am just setting a bad example as a moderator for future forum users :laughing:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.