From a216c156ab59b4a66c6a977924ea2ce1af29a072 Mon Sep 17 00:00:00 2001 From: ogbry Date: Tue, 28 May 2019 15:05:06 +0800 Subject: [PATCH] submission --- practice.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- user.json | 4 ++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/practice.js b/practice.js index a9e0f69..398d05e 100644 --- a/practice.js +++ b/practice.js @@ -27,7 +27,12 @@ Then invoke the callback function, passing in the first element in the array as it's argument. */ -// Code Here +// Code Here + function first(arr, callback){ + callback(arr[0]); + } + + // Do not edit the code below. var names = ['Aodhan', 'Greg', 'Jake', 'Oscar', 'Aodhan', 'Tanner', 'Greg']; @@ -49,6 +54,10 @@ first(names, function(firstName){ //Code Here +function last(arr, callback){ + callback(arr.pop()); +} + // Do not edit the code below. last(names, function(lastName){ console.log('The last name in names is ' + lastName); @@ -67,6 +76,10 @@ last(names, function(lastName){ //Code Here + function multiply(num1, num2, callback){ + callback(num1 * num2); + } + // Do not edit the code below. multiply(4, 3, function(answer){ console.log('The answer is ' + answer); //should console.log "The answer is 12" @@ -86,6 +99,17 @@ multiply(4, 3, function(answer){ //Code Here + function contains(arr, name, callback){ + arr.forEach(element => { + if(element == name){ + callback(true); + } + else{ + callback(false); + } + }); + } + // Do not edit the code below. contains(names, 'Oscar', function(result){ if(result === true){ @@ -107,6 +131,10 @@ contains(names, 'Oscar', function(result){ //Code Here + function uniq(arr, callback){ + callback(arr); + } + // Do not edit the code below. uniq(names, function(uniqArr){ console.log('The new names array with all the duplicate items removed is ', uniqArr); @@ -124,6 +152,13 @@ uniq(names, function(uniqArr){ //Code Here + function each(arr, callback){ + arr.forEach(element => { + let index = arr.indexOf(element); + callback(element, index); + }); + } + // Do not edit the code below. each(names, function(item, indice){ console.log('The item in the ' + indice + ' position is ' + item) @@ -141,6 +176,18 @@ each(names, function(item, indice){ // Code here + function getUserById( + arr, + id, + callback + ){ + arr.forEach(element => { + if(element.id == id){ + callback(element); + } + }); + } + // Do not edit the code below. var users = [ { @@ -166,4 +213,4 @@ var users = [ getUserById(users, '16t', function(user){ console.log('The user with the id 16t has the email of ' + user.email + ' the name of ' + user.name + ' and the address of ' + user.address); }); -// Do not edit the code above. +// Do not edit the code above. \ No newline at end of file diff --git a/user.json b/user.json index 4ac80a0..bb5fcd1 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" + "name": "Bryan A. Alfuente", + "email": "bryan.alfuente@boom.camp" }