From 5e1f46ca2940a34bde72d1457aa5ed53b644be01 Mon Sep 17 00:00:00 2001 From: Franco Date: Wed, 16 Oct 2019 02:52:14 -0400 Subject: [PATCH 1/2] practice-callbacks --- practice.js | 38 ++++++++++++++++++++++++++++++++++---- user.json | 4 ++-- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/practice.js b/practice.js index a9e0f69..0411a5c 100644 --- a/practice.js +++ b/practice.js @@ -28,7 +28,14 @@ */ // Code Here +// var ourArr = [1,2,3]; +function first(array, callback){ + for(i in array){ + callback(array[0], i); + } +} +// first(ourArr); // Do not edit the code below. var names = ['Aodhan', 'Greg', 'Jake', 'Oscar', 'Aodhan', 'Tanner', 'Greg']; @@ -48,6 +55,11 @@ first(names, function(firstName){ */ //Code Here +function last(array, callback){ + for(i in array){ + callback(array[array.length-1], i); + } +} // Do not edit the code below. last(names, function(lastName){ @@ -66,7 +78,9 @@ last(names, function(lastName){ */ //Code Here - +function multiply(x, n, callback){ + callback(x*n); +} // Do not edit the code below. multiply(4, 3, function(answer){ console.log('The answer is ' + answer); //should console.log "The answer is 12" @@ -85,7 +99,9 @@ multiply(4, 3, function(answer){ */ //Code Here - +function contains(array, names, callback){ + array.indexOf('Oscar') > -1 ? callback(true) : callback(false); +} // Do not edit the code below. contains(names, 'Oscar', function(result){ if(result === true){ @@ -106,7 +122,10 @@ contains(names, 'Oscar', function(result){ */ //Code Here - +function uniq(array, callback){ + var thisArr = array.filter((item, index) => array.indexOf(item) === index); + callback(thisArr); +} // Do not edit the code below. uniq(names, function(uniqArr){ console.log('The new names array with all the duplicate items removed is ', uniqArr); @@ -123,7 +142,11 @@ uniq(names, function(uniqArr){ */ //Code Here - +function each(names, callback){ + for(i in names){ + callback(names[i], i); + } +} // Do not edit the code below. each(names, function(item, indice){ console.log('The item in the ' + indice + ' position is ' + item) @@ -140,6 +163,13 @@ each(names, function(item, indice){ */ // Code here +function getUserById(array, id, callback){ + for(i in array){ + if(array[i].id === id){ + callback(array[i]); + } + } +} // Do not edit the code below. var users = [ diff --git a/user.json b/user.json index 4ac80a0..987ce8f 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" + "name": "Francisco R. Ifurung", + "email": "francisco.ifurung@boom.camp" } From bd53caf24e6aae907c799bec337eda7dbebdba64 Mon Sep 17 00:00:00 2001 From: Franco Date: Wed, 16 Oct 2019 03:28:13 -0400 Subject: [PATCH 2/2] practice-callbacks --- practice.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/practice.js b/practice.js index 0411a5c..8c073a8 100644 --- a/practice.js +++ b/practice.js @@ -31,7 +31,7 @@ // var ourArr = [1,2,3]; function first(array, callback){ for(i in array){ - callback(array[0], i); + callback(array[0]); } } @@ -57,7 +57,7 @@ first(names, function(firstName){ //Code Here function last(array, callback){ for(i in array){ - callback(array[array.length-1], i); + callback(array[array.length-1]); } } @@ -99,8 +99,10 @@ multiply(4, 3, function(answer){ */ //Code Here -function contains(array, names, callback){ - array.indexOf('Oscar') > -1 ? callback(true) : callback(false); +function contains(array, name, callback){ + for(i in array){ + array[i] == name ? callback(true) : callback(false); + } } // Do not edit the code below. contains(names, 'Oscar', function(result){ @@ -143,7 +145,7 @@ uniq(names, function(uniqArr){ //Code Here function each(names, callback){ - for(i in names){ + for(var i = 0; i