I NEED TO COME UP WITH A COMPLETE CODE IN JAVASCRIPT WITHOUT CHANGING THE FUNCTI
ID: 3725965 • Letter: I
Question
I NEED TO COME UP WITH A COMPLETE CODE IN JAVASCRIPT WITHOUT CHANGING THE FUNCTIONS PROVIDED. (COPY AND PASTE IN ORDER TO GET THE LINE NUMBERS)
//Begin - pre-built array processing functions - Do not modify
//Calculate the square of a numeric value
//Parameter: Number to be squared
function squareIt(squareMe){
return squareMe * squareMe
}
//Display the elements of an array
//Parameters: array element, index, and name
function arrDisplay(el, index, arr){
//Why is calculating the array length
//each time the function is called inefficient?
var lastIndex = arr.length - 1;
if (index !== lastIndex){
document.write(el + ", ");
} else {
document.write(el + "<br><br>");
}
}
//Used by the array sort method to sort
//numeric values
//Parameters: numbers to be compared
function compareNumbers(a, b) {
return a - b;
}
//Display page header
document.write("<h1>Lab 3: Arrays</h1>");
//End pre-built array processing functions - Do not modify anything above this line
//Modify line 61 to create the even roots array
//with even values 2 through 10
var evenRoots = ;
//Modify line 66 to build the
//even squares array using the array map method
//and the pre-built squareIt function.
var evenSquares = evenRoots.map();
//On line 72 use the array forEach method
//and the pre-built arrDisplay function
//to display the evenSquares array values
document.write("Even Squares: ");
//Modify line 76 to create the odd roots array
//with odd values 1 through 9
var oddRoots = ;
//Modify line 81 to build the
//odd squares array using the array map method
//and the pre-built squareIt function.
var oddSquares = oddRoots.map();
//On line 87 use the array forEach method
//and the pre-built arrDisplay function
//to display the oddSquares array values
document.write("Odd Squares: ");
//Modify line 91 to create the allSquares array by
//concatenating the evenSquares and oddSquares arrays
var allSquares = ;
//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: ");
//Modify line 112 to reverse the allSquares
//array by using the array reverse method.
allSquares.;
//On line 119 use the array forEach method
//and the pre-built arrDisplay function
//to display the reversed allSquares array values.
document.write("All Squares reversed: ");
Explanation / Answer
//Calculate the square of a numeric value
//Parameter: Number to be squared
function squareIt(squareMe){
return squareMe * squareMe
}
//Display the elements of an array
//Parameters: array element, index, and name
function arrDisplay(el, index, arr){
//Why is calculating the array length
//each time the function is called inefficient?
var lastIndex = arr.length - 1;
if (index !== lastIndex){
document.write(el + ", ");
} else {
document.write(el + "<br><br>");
}
}
//Used by the array sort method to sort
//numeric values
//Parameters: numbers to be compared
function compareNumbers(a, b) {
return a - b;
}
//Display page header
document.write("<h1>Lab 3: Arrays</h1>");
//End pre-built array processing functions - Do not modify anything above this line
//Modify line 61 to create the even roots array
//with even values 2 through 10
var evenRoots = [2, 4,6,8,10];
//Modify line 66 to build the
//even squares array using the array map method
//and the pre-built squareIt function.
var evenSquares = evenRoots.map(x=>squareIt(x));
//On line 72 use the array forEach method
//and the pre-built arrDisplay function
//to display the evenSquares array values
document.write("Even Squares: ");
evenSquares.forEach(function(el){
document.write(el);
});
//Modify line 76 to create the odd roots array
//with odd values 1 through 9
var oddRoots = [1, 3, 5, 7, 9];
//Modify line 81 to build the
//odd squares array using the array map method
//and the pre-built squareIt function.
var oddSquares = oddRoots.map(x=>squareIt(x));
//On line 87 use the array forEach method
//and the pre-built arrDisplay function
//to display the oddSquares array values
document.write("Odd Squares: ");
oddSquares.forEach(function(el){
document.write(el);
})
//Modify line 91 to create the allSquares array by
//concatenating the evenSquares and oddSquares arrays
var allSquares = evenSquares.concat(oddSquares);
//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(el){
document.write(el);
});
//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(el){
document.write(el);
})
//Modify line 112 to reverse the allSquares
//array by using the array reverse method.
allSquares.reverse();
//On line 119 use the array forEach method
//and the pre-built arrDisplay function
//to display the reversed allSquares array values.
document.write("All Squares reversed: ");
allSquares.forEach(function(el){
document.write(el);
})
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.