skip to Main Content

I have two integers (integerOne and integerTwo) and I need to write a JavaScript program that outputs the larger one in the console.

The integers are defined like so:

let integerOne = 8; 
let integerTwo = 10;

And I’m expecting 10 to be printed as the output. How do I do this?

3

Answers


  1. We can use the > operator to return a boolean, and depending on the boolean we can use the ternary operator to print the output.

    let integerOne = 8; 
    let integerTwo = 10;
    console.log(integerOne > integerTwo ? integerOne : integerTwo);
    
    Login or Signup to reply.
  2. you can use a simple comparison statement to compare values. for example:

    let integerOne = 8;
    let integerTwo = 10;
    
    if (integerOne > integerTwo) {
      console.log(integerOne);
    } else {
      console.log(integerTwo);
    }
    

    The above example will print 10 on your console.

    Login or Signup to reply.
  3. Use Math.max:

    let i = 8; 
    let j = 10;
    
    console.log(
      Math.max(i, j) // => 10
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search