2

I have a exercise where i need to take two inputs and check if they both equal to one.

If yes console.log positive else console.log false

I tried something like this:

function isPositive(firstNum, secondNum) {
  if (firstNum !== 1) {
    console.log('no positive')
  } else if (secondNum === 1) {
    console.log('positive')
  }
}

isPositive(5, 7)
isPositive(5, 10)
isPositive(5, 6)
isPositive(5, 1)
isPositive(1, 1)

and then when i run it in chrome console this

isPositive(5, 7)
not positive
undefined

isPositive(5, 10) 
not positive
undefined

isPositive(5, 6) 
not positive
undefined

isPositive(5, 1) 
not positive
undefined

isPositive(1, 1)
 yes positive
undefined
4
  • Your code seems to work Commented Nov 5, 2020 at 10:49
  • 1
    your code is working properly! The undefined pops up by the browser itself! try to write var i = 1; in the console and you will see undefined too ;) Commented Nov 5, 2020 at 10:52
  • aw cool thank you guys :) Commented Nov 5, 2020 at 10:52
  • Yes that's why! Anyway your code won't work in this case: isPositive(1, 2); Commented Nov 5, 2020 at 10:57

2 Answers 2

2

Your code is working properly except for the case:

firstNum = 1
secondNum > 1

because won't enter in the first if and nither in the else. By the way you can use the && operator to say if first is true AND second is true then...

function isPositive(firstNum, secondNum){
   if (firstNum === 1 && secondNum === 1) {
        console.log('positive')
   } else {
        console.log('no positive')
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

undefined is return because you do not explicitly return something on that function. If we would do some refactoring, you will see, that it is works as suppossed.

function isPositive(a, b) {
  return (a === 1) && (b === 1) ? 'positive' : false
}

console.log(isPositive(5, 7))
// => false
console.log(isPositive(5, 10))
// => false
console.log(isPositive(5, 6))
// => false
console.log(isPositive(5, 1))
// => false
console.log(isPositive(1, 1))
// => 'positive'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.