代码:
function highAndLow(numbers){
numbers = numbers.split(" ");
var biggest = numbers[0];
for (i = 0; i < numbers.length; i++) {
if (numbers[i] > biggest) {
biggest = numbers[i];
}
}
var smallest = numbers[0];
for (i = 0; i < numbers.length; i++) {
if (numbers[i] < smallest) {
smallest = numbers[i];
}
}
return biggest + " " + smallest;
}
console.log(highAndLow("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6"));
答案 0 :(得分:1)
这是因为它将数字作为字符串而不是数字进行比较。在比较它们之前,您需要convert them to numbers使用Number()
函数将它们转换为数字,例如:
biggest = Number(numbers[0]);