比较整数在JS中

huangapple go评论56阅读模式
英文:

Comparing integers in JS

问题

我需要编写一个JavaScript程序,在控制台输出较大的整数。这两个整数分别定义为:

let integerOne = 8;
let integerTwo = 10;

我期望的输出是10。你应该如何做到这一点?

英文:

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?

答案1

得分: 1

我们可以使用>运算符返回一个布尔值,然后根据这个布尔值使用三元运算符来打印输出。

let integerOne = 8; 
let integerTwo = 10;
console.log(integerOne > integerTwo ? integerOne : integerTwo);
英文:

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);

答案2

得分: 1

Pretty simple using ternary operator:

let integerOne = 8; 
let integerTwo = 10;

integerOne > integerTwo ? console.log(integerOne) : console.log(integerTwo);
英文:

Pretty simple using ternary operator:

let integerOne = 8; 
let integerTwo = 10;

integerOne > integerTwo ? console.log(integerOne) : console.log(integerTwo);

答案3

得分: 0

你可以使用一个简单的比较语句来比较数值。例如:

let integerOne = 8;
let integerTwo = 10;

if (integerOne > integerTwo) {
  console.log(integerOne);
} else {
  console.log(integerTwo);
}

上面的示例将在你的控制台上打印出 10。

英文:

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.

答案4

得分: 0

使用 Math.max

let i = 8;
let j = 10;

console.log(
  Math.max(i, j) // => 10
)
英文:

Use Math.max:

let i = 8; 
let j = 10;

console.log(
  Math.max(i, j) // => 10
)

huangapple
  • 本文由 发表于 2023年7月13日 12:11:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定