英文:
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
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论