2

I need to make a function that returns the lowest number in my array, I can't seem to figure this out and the other questions on here didn't help at all because they gave answers with code i had never seen before, so i have this but it always just returns "0" not 80, so can you help me with my code?

var value = [100, 90, 100, 80];

function lowestGrade(a) {
  for (var i = 0; i < a.length; i++) {
    if (a[i] < a.length);
    value = i; {
      return (value);
    }
  }
}

document.write(lowestGrade(value));
Tarik
  • 79,711
  • 83
  • 236
  • 349
Mark
  • 41
  • 4
  • 1
    What is that semi-colon doing at the end of the `if(...);` ??? – Soren Sep 26 '14 at 01:46
  • Why is code you've never seen bad? There's a builtin way to do this... – elclanrs Sep 26 '14 at 01:48
  • value.reduce(function(a,b){return a < b ? a : b}); – Zack Argyle Sep 26 '14 at 01:48
  • If you know the array is only numbers, then: `value.slice().sort(function(a,b){return a-b})[0]` will do, but you may need to guard against non–numeric values like *null*. Zack's comment is much more efficient though (but requires ES5). – RobG Sep 26 '14 at 02:09

6 Answers6

1

As is already mentioned, there are built in ways to handle this. However, I'm assuming this is some kind of homework problem.

var array = [90, 1, 77, -5, 101];

function lowest(arr) {
  //Assume first is lowest
  var low = arr[0];

  for (var i = 0; i < arr.length; i++) {
    //If this isn't lower, swap
    if (low > arr[i]) {
      low = arr[i];
    }
  }

  //Will always contain lowest number
  return low;
}

document.write(lowest(array));
Josh
  • 44,706
  • 7
  • 102
  • 124
  • Please use `document.write()` instead of `console.log()`. Otherwise it only outputs the results into developer console instead. – Tarik Sep 26 '14 at 01:52
  • @Tarik - Fixed. Also sounds like a feature request. I would have expected them to override and intercept console logging statements for the purposes of snippets. Oh well... – Josh Sep 26 '14 at 01:54
  • I agree. It was also my first impression and expectation, but didn't turn out as I expected. – Tarik Sep 26 '14 at 01:56
  • You are assuming a contiguous array, what if `arr[i]` doesn't exist? – RobG Sep 26 '14 at 01:57
  • @RobG - Sure, there are all kinds of things that could be wrong. What if arr[i] is an Object, or a String... What if `arr` isn't defined, or an Array at all? I assumed this was a homework problem and not going to be running on production somewhere :) – Josh Sep 26 '14 at 02:00
1

Problems are here;

function lowestGrade(a) {
  for (var i = 0; i < a.length; i++) {
    if (a[i] < a.length); // < -- this is an if statement which has no effect
    value = i;  // <-- this just set the value to the loop index i 
    {
      return (value);   // << -- this returns at the first loop where i is zeo 
    }
  }
}

Instead, do;

var value = [100, 90, 100, 80];
function lowestGrade(a) {
  var lowest = a[0];
  for (var i = 0; i < a.length; i++) {
    if (a[i] < lowest) 
       lowest = a[i];
  }
  return lowest;
}

document.write(lowestGrade(value));
Soren
  • 14,402
  • 4
  • 41
  • 67
1
var value = [100, 90, 100, 80];

function lowestGrade(a) {
var lowestNum = a[0]; // Set it to first element
  for (var i = 1; i < a.length; i++) { //loop through array, starting at first element if length is greater than 1
    if (a[i] < lowestNum)
        lowestNum = a[i]; //if the array value is lower than your variable, set your variable to it
  }
  return lowestNum;
}

document.write(lowestGrade(value));
Stan Shaw
  • 3,014
  • 1
  • 12
  • 27
0

Try this:

var value = [100, 90, 100, 80];
lowestGrade(value);

function lowestGrade (value) 
{
    var lowest;
    for(var i = 0, cnt = value.length; i<cnt; i++){
        if(typeof lowest === 'undefined') {
           lowest = value[i]
        } else {
            if(value[i] < lowest){
                lowest = value[i];
            }
        }
    }

    console.log(lowest);
}

Fiddle here

jpcanamaque
  • 1,049
  • 1
  • 8
  • 14
0

Since you are specifically asking for not using Math object in Javascript, there is an answer I have found here in Stackoverflow that solves it in the way you want:

function mymax(a)
{
    var m = -Infinity, i = 0, n = a.length;

    for (; i != n; ++i) {
        if (a[i] > m) {
            m = a[i];
        }
    }

    return m;
}

I don't own this answer, so please check this link to see the orginal author's answer: https://stackoverflow.com/a/13794386/44852

Community
  • 1
  • 1
Tarik
  • 79,711
  • 83
  • 236
  • 349
0

If you are not learning how to sort, you can use native reduce function.

var value = [100, 90, 100, 80];
var min = value.reduce(function (pv,cv) {
    return pv > cv?cv:pv
});
document.write(min);
Mritunjay
  • 25,338
  • 7
  • 55
  • 68