I NEED TO COME UP WITH A COMPLETE CODE IN JAVASCRIPT WITHOUT CHANGING THE FUNCTI
ID: 3730795 • Letter: I
Question
I NEED TO COME UP WITH A COMPLETE CODE IN JAVASCRIPT WITHOUT CHANGING THE FUNCTIONS PROVIDED.
//On line 98 to use the array forEach method
//and the pre-built arrDisplay function
//to display the allSquares array
document.write("All Squares: ");
//Modify line 102 to use the pre-built compareNumbers function
//to sort the allSquares array in ascending order
allSquares.sort();
//On line 108 use the array forEach method
//and the pre-built arrDisplay function
//to display the sorted allSquares array
document.write("All Squares sorted: ");
Explanation / Answer
function arrDisplay(el, index, arr){
var lastIndex = arr.length - 1;
if (index !== lastIndex){
document.write(el + ", ");
} else {
document.write(el + "<br><br>");
}
}
function compareNumbers(a, b) {
return a - b;
}
//On line 98 to use the array forEach method
//and the pre-built arrDisplay function
//to display the allSquares array
document.write("All Squares: ");
allSquares.forEach(function(element, index){
arrDisplay(element, index, allSquares);
});
//Modify line 102 to use the pre-built compareNumbers function
//to sort the allSquares array in ascending order
allSquares.sort(compareNumbers);
//On line 108 use the array forEach method
//and the pre-built arrDisplay function
//to display the sorted allSquares array
document.write("All Squares sorted: ");
allSquares.forEach(function(element, index){
arrDisplay(element, index, allSquares);
});
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.