From d76578dfdce6f94918dc7fcdcc6d2a36fc7a2e5d Mon Sep 17 00:00:00 2001 From: Robby Ann Eslava Date: Tue, 28 May 2019 15:26:27 +0800 Subject: [PATCH 1/2] Javascript-4-Callbacks Submission --- practice.js | 43 ++++++++++++++++++++++++++++++++++++++++--- user.json | 6 +++--- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/practice.js b/practice.js index a9e0f69..2b68c8c 100644 --- a/practice.js +++ b/practice.js @@ -29,6 +29,10 @@ // Code Here +function first(array, callback) { + callback(array[0]) + } + // Do not edit the code below. var names = ['Aodhan', 'Greg', 'Jake', 'Oscar', 'Aodhan', 'Tanner', 'Greg']; @@ -47,8 +51,10 @@ first(names, function(firstName){ Then invoke the callback, passing in the last element in the array as the argument. */ -//Code Here - +function last(array, callback) { + callback(array[array.length - 1]) + } + // Do not edit the code below. last(names, function(lastName){ console.log('The last name in names is ' + lastName); @@ -67,6 +73,10 @@ last(names, function(lastName){ //Code Here +function multiply (num1, num2, callback) { + return 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 +96,15 @@ multiply(4, 3, function(answer){ //Code Here +function contains(array, names, callback) { + if (array.includes(names)) { + callback(true); + } + else { + callback(false); + } + } + // Do not edit the code below. contains(names, 'Oscar', function(result){ if(result === true){ @@ -107,6 +126,10 @@ contains(names, 'Oscar', function(result){ //Code Here +function uniq (array, callback) { + + } + // 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 +147,12 @@ uniq(names, function(uniqArr){ //Code Here +function each (array, callback) { + for (let i = 0; i < array.length; i++) { + callback(array[i], i); + } +} + // Do not edit the code below. each(names, function(item, indice){ console.log('The item in the ' + indice + ' position is ' + item) @@ -141,6 +170,14 @@ each(names, function(item, indice){ // Code here +function getUserById (array, id, callback) { + for (var i = 0; i < array.length; i++) { + if (id == array[i]) { + } + return callback(array[i]); + } +} + // Do not edit the code below. var users = [ { @@ -166,4 +203,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..2a76775 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" -} + "name": "Robby Ann Eslava", + "email": "robbyann.eslava@boom.camp" +} \ No newline at end of file From e60cad3651d1a71150926998a3a151d9981e8115 Mon Sep 17 00:00:00 2001 From: Robby Ann Eslava <50604868+rbbynnslv@users.noreply.github.com> Date: Tue, 28 May 2019 15:29:34 +0800 Subject: [PATCH 2/2] Update practice.js --- practice.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/practice.js b/practice.js index 2b68c8c..31ce541 100644 --- a/practice.js +++ b/practice.js @@ -127,7 +127,9 @@ contains(names, 'Oscar', function(result){ //Code Here function uniq (array, callback) { - + var duplicate = + array.filter(function(cur, ind, array){return array.indexOf(cur) == ind}); + callback (duplicate); } // Do not edit the code below. @@ -203,4 +205,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. \ No newline at end of file +// Do not edit the code above.