From d0be5c3ad6754ae03edaa468ca4a074a28fa45b0 Mon Sep 17 00:00:00 2001 From: echoaldemo Date: Tue, 28 May 2019 14:33:04 +0800 Subject: [PATCH] callbacks done --- practice.js | 45 +++++++++++++++++++++++++++++++++++++-------- user.json | 4 ++-- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/practice.js b/practice.js index a9e0f69..6fe5dbb 100644 --- a/practice.js +++ b/practice.js @@ -27,7 +27,9 @@ Then invoke the callback function, passing in the first element in the array as it's argument. */ -// Code Here +function first(arr, cb) { + cb(arr[0]) +} // Do not edit the code below. var names = ['Aodhan', 'Greg', 'Jake', 'Oscar', 'Aodhan', 'Tanner', 'Greg']; @@ -47,7 +49,9 @@ first(names, function(firstName){ Then invoke the callback, passing in the last element in the array as the argument. */ -//Code Here +function last(arr, cb) { + cb(arr[arr.length-1]) +} // Do not edit the code below. last(names, function(lastName){ @@ -65,7 +69,9 @@ last(names, function(lastName){ Invoke the callback, passing in the product of the two numbers multiplied as the argument. */ -//Code Here +function multiply(num1, num2, cb) { + cb(num1 * num2) +} // Do not edit the code below. multiply(4, 3, function(answer){ @@ -84,7 +90,13 @@ multiply(4, 3, function(answer){ If the name does not exist, invoke the callback with false as the argument. */ -//Code Here +function contains(arr, name, cb){ + for (i in arr){ + if (arr[i] === name) + cb(true) + } + cb(false) +} // Do not edit the code below. contains(names, 'Oscar', function(result){ @@ -105,7 +117,16 @@ contains(names, 'Oscar', function(result){ Remove any duplicate values from the array, and invoke the callback with the modified array as an argument. */ -//Code Here +function uniq(arr, cb) { + let unique_array = [] + for(let i = 0;i < arr.length; i++){ + if(unique_array.indexOf(arr[i]) == -1){ + unique_array.push(arr[i]) + } + } + cb(unique_array) +} + // Do not edit the code below. uniq(names, function(uniqArr){ @@ -122,7 +143,11 @@ uniq(names, function(uniqArr){ For each name in the array, invoke the callback and pass in the name and the name's index as arguments. */ -//Code Here +function each(arr, cb){ + arr.forEach(function(element) { + cb(element, arr.indexOf(element)) + }); +} // Do not edit the code below. each(names, function(item, indice){ @@ -131,7 +156,6 @@ each(names, function(item, indice){ // Do not edit the code above. - ////////// PROBLEM 7 ////////// /* @@ -139,7 +163,12 @@ each(names, function(item, indice){ When the correct user object is found, invoke the callback with the user object as an argument. */ -// Code here +function getUserById(arr, id, cb){ + arr.forEach(function (element) { + if (element.id === id) + cb(element) + }); +} // Do not edit the code below. var users = [ diff --git a/user.json b/user.json index 4ac80a0..f978505 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" + "name": "Jericho Aldemo", + "email": "jericho.aldemo@boom.camp" }