3

I know a solution to this using Math.max(), but rather I was hoping someone could explain why the following doesn't work.

array = [4, 5, 1, 3]

for (var i = 0; i < array.length; i++) {
  var num = 0;  
  if (array[i]> num) {
    num = array[i];
  }
}

The above just gives the last number in the array as num. So my guess is that array[i] isn't appearing as a number (but perhaps as a string) in the conditional. Please forgive my incomplete understanding of JS. Any help is much appreciated!

3
  • 3
    Because you have declare num inside for loop, that's why it will set to 0 for every iteration. Declare num outside of the loop, it will work. Commented Feb 9, 2018 at 20:03
  • 2
    Math.max(...array); or array.reduce((acc,itm)=>itm>acc?itm:acc,0) Commented Feb 9, 2018 at 20:07
  • @Iwrestledabearonce. did you read this: I know a solution to this using Math.max() Commented Feb 9, 2018 at 20:13

3 Answers 3

4

You keep resetting num to 0 in your loop. You need to move the variable declaration and assignment outside:

array = [4, 5, 1, 3]

var num = 0;  
for (var i = 0; i < array.length; i++) {
  if (array[i]> num) {
    num = array[i];
  }
}
console.log(num);

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I should have known that though. Maybe I'll delete this question. hangs head in shame
I would suggest not deleting the question - you never know when it might be helpful to someone else. If you do decide to delete it, first take a look at my answer where I describe how to debug your code in the browser developer tools, and try it out right there using the snippet I posted. I think it will help you a lot.
4

The real answer to your question is to learn how to debug JavaScript code. Your web browser has a debugger built in that will let you step through your code statement by statement and view all your variables each step of the way. Please try this; it will help you immensely.

Simply add a debugger; statement before the first line of code, then open the developer tools and reload your page. The debugger will stop on that statement, and then use the Step In button in the debugger to single step through the code.

To make it easy for you to get started, here is a copy of your original code (with the bug) as a snippet with the debugger; statement added:

function test() {
  debugger;

  array = [4, 5, 1, 3]

  for (var i = 0; i < array.length; i++) {
    var num = 0;  
    if (array[i]> num) {
      num = array[i];
    }
  }
}
 
test();

It's best to put the code you're testing inside a function as I did here. That lets you look at your local variables without the standard global variables and functions cluttering the view.

Open the developer tools and then run the snippet, then use the single step button as I mentioned above.

Every browser has developer tools built in that work in a similar way. For example, here is a guide to the Chrome DevTools.

2 Comments

That's pretty cool thanks for the tip. It's sure to come in handy.
I'm glad this was helpful! You will find all sorts of useful features in the developer tools, but the basic JavaScript debugging is a great place to start. Happy hacking!
1

Just some hints:

  • Declare all variables on top.
  • Use for getting some min or max value the first value as start and value for comparison.
  • Iterate from the second item, because you already took the first item.

var array = [4, 5, 1, 3],
    i,
    max = array[0];

for (i = 1; i < array.length; i++) {
    if (max < array[i]) {
        max = array[i];
    }
}

console.log(max);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.