"var sb=1" Basic Algorithm Scripting: Return Largest Numbers in Arrays


Can I know why the suggested solution used “var sb=1” instead of “var sb=0” as index count always starts from 0?
My code works with “var sb=0”, would like to know the reason behind “var sb=1”? thanks!

Your code so far

function largestOfFour(arr) {
var result = [];
for (var n = 0; n<arr.length; n++) {
var largestNumber = arr[n][0];
for (var sb=0; sb<arr[n].length; sb++) {
  if (arr[n][sb]>largestNumber) {
} result[n] = largestNumber;

return result;

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

if you start with sb = 0 you will compare the first item in the array to itself

starting from sb = 1 remove an unnecessary check

1 Like

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

Noted, thank you! :slight_smile: