plaidshirt
plaidshirt

Reputation: 5701

Highest value of an array

I have an array, which contains several numbers, like this:

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

I would like to get smallest and greatest number from this array. It seems pretty easy task, but accidently I get wrong output for code below.

function highAndLow(numbers){
var args = Array.prototype.slice.call(arguments);
var m = 0, i = 0, n = args.length;

console.log(args.length)

    for (i=0; i < n; i++) {
        if (args[i] > m) {
            m = args[i];
            console.log(m)
        }
    }

return m

}

It says length is 1 and return value is 0.

Upvotes: 2

Views: 231

Answers (7)

Henrik Aronsson
Henrik Aronsson

Reputation: 1413

You are passing a string as parameter to your function.

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

should be replace with

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

to actually pass an array with numbers.

Upvotes: 2

Krutika Patel
Krutika Patel

Reputation: 322

You can try this code.

 <script type="text/javascript">

 function highAndLow(numbers) 
 {

                var args = numbers.split(" ");                    
                console.log(args.length)
                var min = Math.min.apply(Math, args),
                max = Math.max.apply(Math, args);
                console.log(min);
                console.log(max);


 }

 highAndLow('4 5 29 54 4 0 -214 542 -64 1 -3 6 -6');
 </script>

Upvotes: 1

Theophilus Omoregbee
Theophilus Omoregbee

Reputation: 2503

The splice.call is returning some thing else not the length of array because it is seeing everything as characters.

This implementation should help out ,

  1. split the string with space using array.split(delimter) Javascript Split
  2. Then convert the splitted array to number as you do the test
  3. We loop through and test for minimum and maximum array value

// Code goes here

 
function highAndLow(numbers){
  var args = numbers.split(" ");
  
  var n= args.length;
  
  document.getElementById("argLength").innerHTML="Array Length:"+n;
  
  var i=0;
  var max = Number(args[i]);
  var min = Number(args[i]);
  
  for(i=1;i<n; i++){
    var num=Number(args[i]);
    
    if(num>max)
        max=num;
  
    if(min>num)   
      min=num;
    
  }
  
  document.getElementById("output").innerHTML=("Min:"+min+", Max:"+max);
  
}
<!DOCTYPE html>
<html>

  <head>
 
  </head>

  <body onload='highAndLow("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6");'>
    
    <h1 id="argLength"></h1>
    
    <h1 id="output"> </h1>
  </body>

</html>

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386883

You have a string as parameter. You need to split first. And convert to Number.

args = numbers.split(' ').map(Number);

A minor hint: You can use the first element as first min/max value and iterate from the second element.

function highAndLow(numbers) {
    var args = numbers.split(' ').map(Number),
        min = args[0],
        max = args[0],
        i,
        n = args.length;

    for (i = 1; i < n; i++) {
        if (args[i] < min) {
            min = args[i];
        }
        if (args[i] > max) {
            max = args[i];
        }
    }
    return { min: min, max: max };
}

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

Upvotes: 9

Emil S. J&#248;rgensen
Emil S. J&#248;rgensen

Reputation: 6366

Here is the simplest way of doing it:

function highAndLow(str) {
  str = str.split(' ').sort(function(a, b) {
    return parseInt(a) - parseInt(b)
  });
  return [str[str.length - 1],
    str[0]
  ];
}
document.write(highAndLow('4 5 29 54 4 0 -214 542 -64 1 -3 6 -6'));

Upvotes: 1

Iceman
Iceman

Reputation: 6145

function highAndLow(data) {
  var data = "4 5 29 54 4 0 -214 542 -64 1 -3 6 -6";
  var dataArr = data.split(' ').map(Number);
  var result = {};
  result.max = Math.max.apply(Math, dataArr);
  result.min = Math.min.apply(Math, dataArr);
  return result;
}
var res = highAndLow("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6")
console.log(res.min);
console.log(res.max);

Upvotes: 1

Jai
Jai

Reputation: 74748

You can use Math.max(), Math.min():

function highAndLow(str){
  var arr = str.split(/\s/);
  var max = Math.max.apply(Math, arr);
  var min = Math.min.apply(Math, arr);
  console.log('Max:::', max, " Min::::", min);
}

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

Upvotes: 5

Related Questions