0

I have these 2 loops to find the highest number in an integer array (a for loop and a while loop that serve the same purpose) For:

var forHighest = function(array) {
  var highest = array[0];
  for (var i = 0; i < array.length; i++) {
    if (highest < array[i]) {
      highest = array[i];
    }
  }
  return highest;
};

While:

var whileHighest = function(array) {
  var i = 0;
  var highest = array[0];
  while (i < array.length) {
    if (array[i] > highest)
      highest = array[i];
    i++;
  }
  return highest;
};

But now I need to make a While loop but everything I try keeps giving me the number at the [0] index of my array. I don't think I've ever used a do...while loop functionally before so maybe I'm not understanding fully how they work. Here are a few of my attempts:

var doWhile = function(array) {
  var i = 0;
  do {
    i++;
  } while (i < array.length);
  if (array[i] > highest)
    highest = array[i];
  return highest;
};

also:

var i = 0;
var highest = array[0];
do{
    if(highest < array[i]){
    highest = array[i];
    i++;
    } 
  } while(i < array.length);

and:

var doWhile = function(array) {
    var i = [i];
//  var i = 0;
    var highest = array[i];
    do
      {
       i++;
      } while (array[i] < highest && i < array.length);
        //  if (array[i] > highest);
        return array[i];

But none of them work. Can someone explain to me what I'm doing wrong?

H. Hirschfeld
  • 73
  • 1
  • 8
  • 2
    Your first do while is completely pointless (does nothing in the loop except increment i) - your second loop will loop forever, because you need to do i++ OUTSIDE the if condition, your third loop makes `i` an array, so, wont work at all ... you're close though ... move the i++ in the second do..while outside the if condition, and you should be golden – Jaromanda X Feb 09 '16 at 04:50
  • If your purpose is to find max and min then you can take a look at this http://stackoverflow.com/questions/1669190/javascript-min-max-array-values – brk Feb 09 '16 at 04:53
  • @sinto - arrays do not have a max/min function (yet?) – Jaromanda X Feb 09 '16 at 04:53
  • @Jaromanda X-sorry about that comment:var arr = [10,0,30,40]; arr.max = function() { return Math.max.apply(Math, this); }; arr.min = function() { return Math.min.apply(Math, this); }; alert("min: " + arr.min() + " max: " + arr.max()); – Sinto Feb 09 '16 at 05:12
  • @sinto, no need to apologise, the arr.min/arr.max code is more clear in the link provided in the comment by user2181397 anyway ... but, I don't think this question is really about getting the min/max of an array – Jaromanda X Feb 09 '16 at 05:15

2 Answers2

1

First up, understand that the do-while loop is not much different than the while loop. The only difference is that do-while is an exit testing loop, whereas the while loop is an entry testing loop.

So, in the do-while the looping criteria i.e., the condition in the while parentheses, is evaluated while exiting the loop. And in order to do exit the loop, it has to enter the loop at least once. Hence, the assured at-least-one-time-run of the block of code in the do-while loop. Other than that the way the while loop works is same as the do-while. Same initialization code. Bringing about the exit criteria inside the loop also remains the same.

Now looking at your code, your while loop (shown below) works fine:

while (i < array.length) {
    if (array[i] > highest)
      highest = array[i];
    i++;
  }

Now, we can rewrite it as the do-while by simply moving the while statement to the end and put the do in its place. Note that the code before the loop (that is, the pre-looping initialization code) needs no change, either.

That is,

var i = 0;
var highest = array[0];
do {
    if (array[i] > highest)
      highest = array[i];
    i++;
  } while (i < array.length);
return highest;

As mentioned by @Jaromanda X in the comment, you should be golden :)

Sarath Chandra
  • 1,850
  • 19
  • 40
1

As mentioned in the comments, there are better ways to get the max/min in an array, but the point of the question is to know how write a do...while loop

Your second do...while was close

var i = 0;
var highest = array[0];
do {
    if (highest < array[i]){
        highest = array[i];
    }
    i++;  // this line moved out of the if block
} while(i < array.length);

However, more correctly

var i = 1;
var highest = array[0];
if (array.length > 1) {
    do {
        if(highest < array[i]){
            highest = array[i];
        }
        i++;  
    } while(i < array.length);
}

because there's no need to check if the array is a single element, nor do you need to check array[0] against array[0], so you can start the loop with i = 1

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • And, thank you too @Jaromanda X - this has rlly cleared up do..while loops for me & some of the details! Thanks, I really appreciate it! – H. Hirschfeld Feb 09 '16 at 07:02