'There are six functions, read each comment and write the return statements for each of them
Hi everyone I am hoping I can get some help here and someone can explain this to me. I am in a coding class and I cannot figure this out for the life of me. This is the code I need to finish. I cannot seem to get this right. Can anyone help? I have tried to add the lines of code needed multiple different ways so I finally restarted everything and I am lost.
// Instructions
// - Given the following 6 empty functions, add the right return statement to each of
them to complete the activity
// Please do not change any of the function names
//This is your starting array
const arr = [10, 10, 16, 12];
function returnFirst(arr) {
// return the first item from the array
}
function returnLast(arr) {
// return the last item of the array
}
function getArrayLength(arr) {
// return the length of the array
}
function incrementByOne(arr) {
// arr is an array of integers(numbers), Increment all items in the array by
// return the array
}
function addItemToArray(arr, item) {
// add the parameter item to the end of the array arr
// return the array
}
function addItemToFront(arr, item) {
// add the parameter item to the front of the array arr
// return the array
// hint: use the array method .unshift
}
//uncomment these lines to check results in browser console
// console.log("returnFirst result:" + returnFirst(arr))
// console.log("returnLast result:" + returnLast(arr))
// console.log("getArrayLength result:" + getArrayLength(arr))
// console.log("incrementByOne result:" + incrementByOne(arr))
// console.log("addItemToArray result:" + addItemToArray(arr, 10))
// console.log("addItemToFront result:" + addItemToFront(arr, 10))
//////////////////////////////////
//don't change this line
if (typeof module !== 'undefined') {
module.exports = {
returnFirst,
returnLast,
getArrayLength,
incrementByOne,
addItemToArray,
addItemToFront,
};
}
Solution 1:[1]
You don't say what language this is but I assume it's javascript. The question is asking you to find the syntax to read or manipulate the initial array and tag it onto the end of a return statement. So the first answer might be something like: return arr[0] which should show the first (zero indexed) item in the array. The whole function then looks like:
function returnFirst(arr) {
// return the first item from the array
return arr[0];
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
