Taking this step by step.
First to get the largest number in an array
function largestOfFour(arr) {
// You can do this!
for(i=0;i<arr.length;i++){
var largest = 0;
if(arr[i]>largest){
largest = arr[i];
}
return largest;
}
return arr;
}
largestOfFour([4, 5, 1, 3]);
Second Step:
function largestOfFour(arr) {
// You can do this!
for(i =0 ; i <arr[i].length; i++){
//The trick is to assign the largest variable correctly.
var largest = 0;
for(k=0;k<4;k++){
if(arr[i][k] > largest){
largest = arr[i][k];
}
}
console.log(largest);
}
return arr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Finally
function largestOfFour(arr) {
// You can do this!
var newarray = [];
for (var i = 0; i < arr.length; i++){
var largest = [];
for(k=0;k<4;k++){
if(arr[i][k] > largest){
largest = arr[i][k];
}
}
newarray[i] = largest;
}
return newarray;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
First to get the largest number in an array
function largestOfFour(arr) {
// You can do this!
for(i=0;i<arr.length;i++){
var largest = 0;
if(arr[i]>largest){
largest = arr[i];
}
return largest;
}
return arr;
}
largestOfFour([4, 5, 1, 3]);
Second Step:
function largestOfFour(arr) {
// You can do this!
for(i =0 ; i <arr[i].length; i++){
//The trick is to assign the largest variable correctly.
var largest = 0;
for(k=0;k<4;k++){
if(arr[i][k] > largest){
largest = arr[i][k];
}
}
console.log(largest);
}
return arr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Finally
function largestOfFour(arr) {
// You can do this!
var newarray = [];
for (var i = 0; i < arr.length; i++){
var largest = [];
for(k=0;k<4;k++){
if(arr[i][k] > largest){
largest = arr[i][k];
}
}
newarray[i] = largest;
}
return newarray;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
No comments:
Post a Comment