javascript - Find largest value in a String of numbers without Math.max() - Stack Overflow

What's wrong in this function, I cannot figure it out: function highAndLow(numbers){var arr = numb

What's wrong in this function, I cannot figure it out:

function highAndLow(numbers){
   var arr = numbers.split(" ");
   var largest = arr[0];
   for(var i = 1; i < arr.length; i++) {
      if(arr[i] > largest) {
         largest = arr[i];
      }
   }

   return largest;
 }

This:

highAndLow("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6");

Returns 6 and it should return 542. Thank you for your help!

What's wrong in this function, I cannot figure it out:

function highAndLow(numbers){
   var arr = numbers.split(" ");
   var largest = arr[0];
   for(var i = 1; i < arr.length; i++) {
      if(arr[i] > largest) {
         largest = arr[i];
      }
   }

   return largest;
 }

This:

highAndLow("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6");

Returns 6 and it should return 542. Thank you for your help!

Share Improve this question asked May 2, 2017 at 15:43 Liviu CraciunLiviu Craciun 92 silver badges6 bronze badges 3
  • 3 You need to convert the strings to numbers so they won’t be pared in alphabetical* order. "20" < "3" because 2 es before 3, but 20 > 3. – Ry- Commented May 2, 2017 at 15:44
  • But that's why I split the string into an array. – Liviu Craciun Commented May 2, 2017 at 15:46
  • you also need to alter the if statement to if(+arr[i] > largest) – Peter Grainger Commented May 2, 2017 at 15:52
Add a ment  | 

4 Answers 4

Reset to default 2

Chain .map(Number) to .split() call to convert string characters to numbers for proper parison to occur within for loop.

function highAndLow(numbers) {
  var arr = numbers.split(" ").map(Number);
  var largest = arr[0];
  for (var i = 1; i < arr.length; i++) {
    if (arr[i] > largest) {
      largest = arr[i];
    }
  }
  return largest;
}

console.log(highAndLow("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6"));

if you are looking for the least code option which is less performant you could use map and sort.

return numbers.split(" ")
              .map(Number)
              .sort(function(a, b){return a-b})
              .pop();

The map will change all the strings to a number: https://developer.mozilla/en/docs/Web/JavaScript/Reference/Global_Objects/Map

The array function sort will sort ascending: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort?v=example

then the pop will take the first value from the array: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop?v=example

You parseInt while paring values.

Code

function highAndLow(numbers){
   var arr = numbers.split(" ");
   var largest = arr[0];
   for(var i = 1; i < arr.length; i++) { 
      if(parseInt(arr[i]) > parseInt(largest)) {
         largest = arr[i];
      }
   }

   return largest;
 }

One solution is to change this:

largest = arr[i];

to this:

largest = +arr[i];

  function highAndLow(numbers){
       var arr = numbers.split(" ");
       var largest = arr[0];
       for(var i = 1; i < arr.length; i++) {
          if(arr[i] > largest) {
             largest = +arr[i];
          }
       }

       return largest;
     }
     console.log(highAndLow("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6"));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745454214a4628410.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信