Question

I posted the following JavaScript question a bit earlier. However. I made the mistake of copying...

I posted the following JavaScript question a bit earlier. However. I made the mistake of copying it twice. So when I pasted it into the question box here on CHEGG it might have been somewhat confusing to read and understand. So here it is again in a much more simple format. I am in coding school and this is the JS II set of questions. Any help would be greatly appreciated !! Basically the answers will just give me an idea as to how these type of functions should be written. Thnx !! Michael

// Create a higher order function and invoke the callback function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems.

const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

/*

// GIVEN THIS PROBLEM:

function firstItem(arr, cb) {

// firstItem passes the first item of the given array to the callback function.

}

// SOLUTION:

function firstItem(arr, cb) {

return cb(arr[0]);

}

// NOTES ON THE SOLUTION:

// firstItem is a higher order function.

// It expects a callback (referred to as `cb`) as its second argument.

// To test our solution, we can use the given `items` array and a variety of callbacks.

// Note how callbacks can be declared separately, or inlined.

// TEST 1 (inlined callback):

const test1 = firstItem(items, item => `I love my ${item}!`);

console.log(test1); // "I love my Pencil!"

// TEST 2 (declaring callback before hand):

function logExorbitantPrice(article) {

return `this ${article} is worth a million dollars!`;

};

const test2 = firstItem(items, logExorbitantPrice);

console.log(test2); // "this Pencil is worth a million dollars!"

*/

function getLength(arr, cb) {

// getLength passes the length of the array into the callback.

}

function last(arr, cb) {

// last passes the last item of the array into the callback.

}

function sumNums(x, y, cb) {

// sumNums adds two numbers (x, y) and passes the result to the callback.

}

function multiplyNums(x, y, cb) {

// multiplyNums multiplies two numbers and passes the result to the callback.

}

function contains(item, list, cb) {

// contains checks if an item is present inside of the given array/list.

// Pass true to the callback if it is, otherwise pass false.

}

/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {

// removeDuplicates removes all duplicate values from the given array.

// Pass the duplicate free array to the callback function.

// Do not mutate the original array.

}

Homework Answers

Answer #1

Author: Rishabh Jain

Date:07/09/2019

Modified : 09/09/2019

Program for remove duplicate function as a higher order function

arr=[1,12,3,2,1,5,3,4,20] //Sample array is taken

//------------------------------------------------------------------------------------------------------------------------------------------------------------

// For Part one

function getLength(arr,cb){

cb(arr.length);

}

//Calling of getLength

getLength(arr,item=>{console.log("Array length is "+item)});

//------------------------------------------------------------------------------------------------------------------------------------------------------------

// For Part two

function last(arr, cb) {

cb(arr[arr.length-1]);

}

//Calling function last

last(arr,ele=>{ console.log("The last element of the array is "+ele) })

//------------------------------------------------------------------------------------------------------------------------------------------------------------

// For Part three

function sumNums(x, y, cb) {

cb(x+y)

}

//Calling function sumNums

sumNums (99,201,sum=>{ console.log("The sum of two numbers is "+sum) })

//------------------------------------------------------------------------------------------------------------------------------------------------------------

// For Part four

function multiplyNums(x, y, cb) {

cb(x*y)

}

//Calling function multiplyNums

multiplyNums(99,201,prod=>{ console.log("The product of two numbers is "+prod) })

//------------------------------------------------------------------------------------------------------------------------------------------------------------

// For Part five

function contains(item, list, cb) {

let isPresent=false;

for(let i=0;i<list.length;++i){

if(list[i]===item){

isPresent=true;

break;

}

}

cb(isPresent);

}

//Calling function contain

contains(5,arr,ans=>{ if(ans){ console.log("The item is present")} else{ console.log("The item is not present")} })

//------------------------------------------------------------------------------------------------------------------------------------------------------------

// For Stretched Part

function print(array){

array.forEach(function (eachName, index){

   console.log(index + 1 + ". " + eachName);

});

}

function removeDuplicates(array, cb) {

dup=[];pos=0;

for(let i=0;i<array.length;++i){

if(i==0){

dup[pos++]=array[i]

}else{

flag=0;

for(let j=0;j<dup.length;++j){

if(dup[j]==array[i]){

flag=1

}

}

if(flag==0){

dup[pos++]=array[i];

}

}

}

cb(dup)

}

removeDuplicates(arr,print)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions