Tchbell - 1 year ago 99

Javascript Question

I am trying to make a function that picks up the largest number in an array, but I can't seem to get it to go past the first number.

`function largestNumber(numbers) {`

var constant = 1;

for(i = 0; i < numbers.length; i++) {

if(numbers[i] > constant) {

constant = numbers[i];

}

return constant;

}

}

var numbers = [3, 4, 2, 6, 45, 775, 83, 5, 7];

largestNumber(numbers);

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

```
function largestNumber(numbers) {
var constant = 1;
for (i = 0; i < numbers.length; i++) {
if (numbers[i] > constant) {
constant = numbers[i];
}
}
return constant; // after iterating the entire array!
}
var numbers = [3, 4, 2, 6, 45, 775, 83, 5, 7];
console.log(largestNumber(numbers))
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**