我正在尝试从给用户的提示中找到最大和最小数目。由于某些原因,似乎只有Math.min可以正常工作,而不是Math.max。为什么会这样?
var userNum = parseInt(window.prompt("Enter five numbers separated by commas"), 10);
window.console.log("The lowest number is: " + Math.min(userNum));
window.console.log("The highest number is: " + Math.max(userNum));
//HIGHEST DOES NOT SEEM TO WORK
答案 0 :(得分:3)
您正在解析一个包含以逗号分隔的数字序列的字符串,但是parseInt不会返回您想要的内容,而是仅返回该序列中的第一个数字。
您应该达到的结果:
var userNum = prompt("Enter five numbers separated by commas").split(',')
// prompt returns a string, you can make an array splitting by commas
// https://www.w3schools.com/jsref/jsref_split.asp
window.console.log("The lowest number is: " + Math.min( ...userNum ));
// spread the array into min() and max() with ... operator
// https://codeburst.io/javascript-es6-the-spread-syntax-f5c35525f754
window.console.log("The highest number is: " + Math.max( ...userNum ));