Write Khan Javascript to find the largest element in an array. Copy the followin
ID: 3773951 • Letter: W
Question
Write Khan Javascript to find the largest element in an array. Copy the following code to Khan and fill in the for loop with an if statement to make it work. Paste the resulting Khan code into the answer box.
//Program to find the largest integer in a list of
//positive integers and print the largest
var listMax = 0; // Keep track of the largest, but start it at 0
// Add code to declare an array with elements 4,8,2,1
//Add for loop with if statement in side
fill (255, 0, 0);
text("The Largest is " + listMax,100,100);
array with 482.1 characters tagsExplanation / Answer
var listMax = 0; // Keep track of the largest, but start it at
// Add code to declare an array with elements 4,8,2,1
var array=[4,8,2,1];
var i=0;
//Add for loop with if statement in side
for(i=0;i < array.length;i++) //loop for each elemnt in array
{
if(listMax < array[i]) //if there is an element greater than listMax
listMax = array[i]; //update listMax
}
alert("largest number" + listMax);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.