problem with an javascript inputs in javascript

Viewed 38

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
2 Answers

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')
   }
}

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'
Related