0

Is this code correct to find the max from an array? There are other ways to find the max, however the requirements are to use "for" loop!Thanks! Full reqs : "Create an array with 10 numbers of your choice and a variable named max. Assign 0 to max. In a for loop, find the greatest number, assign it to max and write it in the console."

var numArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var max = 0;
var arrLen = numArray.length;
for (i = 0; i < arrLen; i++) {
 if (numArray[i] - numArray[arrLen - 1] >= 0) {
 max = numArray[i];
 } else {
 continue;
 }
 console.log(max);
} 
asked Mar 1, 2020 at 16:25
4
  • 1
    You can simply use: Math.max(...numArray) Commented Mar 1, 2020 at 16:27
  • @palaѕн - "...the requirements are to use "for" loop...". Commented Mar 1, 2020 at 16:28
  • ok, then maybe this solution Commented Mar 1, 2020 at 16:29
  • I think this's a homework Commented Mar 1, 2020 at 16:29

2 Answers 2

2

Approach it like this:

  1. Start with max at -Infinity.
  2. Inside the loop body, if numArray[i] is greater than max, set max to numArray[i].
  3. Show the result after the loop, since while the loop is still running, max may not be the maximum value yet.

At the end of the loop, max will contain the highest value in the array (or -Infinity if the array is empty).


Don't forget to declare all of your variables, including i.

answered Mar 1, 2020 at 16:28
3
  • Thnanks TJ, however requirements are: max= 0; & use a "for" loop. See below: "Create an array with 10 numbers of your choice and a variable named max. Assign 0 to max. In a for loop, find the greatest number, assign it to max and write it in the console." I know about max/ and function to sort the array, however I need to follow reqs. Thoughts? Thanks! Commented Mar 1, 2020 at 16:36
  • @AlexCocan - The suggestion above is following those requirements. (Other than if the requirements really require you to display max from within the loop, but I think that's a misreading as it makes no sense to do that...) Commented Mar 1, 2020 at 17:57
  • Thanks TJ, I used @ninascholz's solution, that works fine for me. Commented Mar 1, 2020 at 20:55
2

You could start with the first element as max value and iterate from index one.

Then check and change max if a new maximum is found.

Do not forget to declare all variables (like i).

var numArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 max = numArray[0],
 arrLen = numArray.length,
 i;
for (i = 1; i < arrLen; i++) {
 if (numArray[i] > max) max = numArray[i];
}
console.log(max);

For having all numbers greater than zero, you could omit the assignment with first value and start index from zero.

var numArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 max = 0,
 arrLen = numArray.length,
 i;
for (i = 0; i < arrLen; i++) {
 if (numArray[i] > max) max = numArray[i];
}
console.log(max);

answered Mar 1, 2020 at 16:29
1
  • Thanks Nina, that works, however reqs are : max = 0; & use "for" loop. Thoughts? Thanks! Commented Mar 1, 2020 at 16:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.