简体   繁体   English

返回给定字符串的最高和最低编号-Javascript

[英]Return the highest and lowest number of a given string - Javascript

In this little assignment you are given a string of space separated numbers, and have to return the highest and lowest number. 在这个小作业中,您将得到一串用空格分隔的数字,并且必须返回最高和最低数字。

I know I can find dozens of solution and examples of this one, but I have a case that I really can not figure it out and I would like to know why. 我知道我可以找到很多解决方案和示例,但是我有一个案例,我真的无法弄清楚,我想知道为什么。

This is the code 这是代码

 highAndLow = numbers => { var smallestElement = numbers[0]; var highestElement = numbers[0]; numbers = numbers.split(' '); for (var i = 0; i < numbers.length; i++) { if (numbers[i] < smallestElement) { smallestElement = numbers[i]; } console.log(`${numbers[i]} > ${highestElement}`); if (numbers[i] > highestElement) { console.log('----------------------------'); highestElement = numbers[i]; } } return `${highestElement} ${smallestElement}`; }; console.log(highAndLow('4 5 29 54 4 0 -214 542 -64 1 -3 6 12')); 

Considering the example above, my result should be 542 -214 , however I get 6 -214 . 考虑上面的示例,我的结果应该是542 -214 ,但是我得到6 -214

I put two console.log that would print ----------------- when conditions is true, but when I look at the output 我放了两个console.log ,当条件为真时,它将打印----------------- ,但是当我查看输出时

在此处输入图片说明

How does if(6 > 542) return true ? if(6 > 542)返回true

The reason you are getting 6 as highest value is because when you are doing if (numbers[i] > highestElement) it is comparing string not a number, and in string 6 is greater than 54 as it compares character by character. 之所以将6作为最大值,是因为在执行if (numbers[i] > highestElement)它在比较字符串而不是数字,并且在字符串6中大于54 ,它逐个字符地进行比较。 so change if (numbers[i] > highestElement) to if (+numbers[i] > +highestElement) (+ will convert string to number) 因此,将if (numbers[i] > highestElement)更改为if (+numbers[i] > +highestElement) (+会将字符串转换为number)

 highAndLow = numbers => { var smallestElement = numbers[0]; var highestElement = numbers[0]; numbers = numbers.split(' '); for (var i = 0; i < numbers.length; i++) { if (+numbers[i] < +smallestElement) { smallestElement = numbers[i]; } console.log(`${numbers[i]} > ${highestElement}`); if (+numbers[i] > +highestElement) { console.log('----------------------------'); highestElement = numbers[i]; } } return `${highestElement} ${smallestElement}`; }; console.log(highAndLow('4 5 29 54 4 0 -214 542 -64 1 -3 6 12')); 

You are comparing string.Instead you can split the string into an array and use map function to convert each element to integer. 您正在比较字符串,而是可以将字符串拆分为数组并使用map函数将每个元素转换为整数。 Then use sort which will arrange the array and then Math.max/min to get the values 然后使用sort来排列数组,然后使用Math.max/min来获取值

 highAndLow = function(x) { // split the string & map function to convert each element to number var newArray = x.split(' ').map(function(item) { return Number(item) }) return { max: Math.max(...newArray), min: Math.min(...newArray), } }; console.log(highAndLow('4 5 29 54 4 0 -214 542 -64 1 -3 6 12')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM