From d8ba60d47915b22e80258c6e5a8e75c32458f152 Mon Sep 17 00:00:00 2001 From: Koji Adriano Jr Date: Tue, 28 May 2019 14:19:39 +0800 Subject: [PATCH 1/4] initialized user.json and 1-2 solved --- practice.js | 87 +++++++++++++++++++++++++++-------------------------- user.json | 6 ++-- 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/practice.js b/practice.js index a9e0f69..161b32f 100644 --- a/practice.js +++ b/practice.js @@ -27,14 +27,16 @@ Then invoke the callback function, passing in the first element in the array as it's argument. */ -// Code Here +function first(array, callback) { + callback(array[0]); +} // Do not edit the code below. var names = ['Aodhan', 'Greg', 'Jake', 'Oscar', 'Aodhan', 'Tanner', 'Greg']; -first(names, function(firstName){ - console.log('The first name in names is ' + firstName); - return firstName; +first(names, function(firstName) { + console.log('The first name in names is ' + firstName); + return firstName; }); // Do not edit the code above. @@ -47,12 +49,14 @@ 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); - return lastName; +last(names, function(lastName) { + console.log('The last name in names is ' + lastName); + return lastName; }); // Do not edit the code above. @@ -68,8 +72,8 @@ last(names, function(lastName){ //Code Here // Do not edit the code below. -multiply(4, 3, function(answer){ - console.log('The answer is ' + answer); //should console.log "The answer is 12" +multiply(4, 3, function(answer) { + console.log('The answer is ' + answer); //should console.log "The answer is 12" }); // Do not edit the code above. @@ -87,12 +91,12 @@ multiply(4, 3, function(answer){ //Code Here // Do not edit the code below. -contains(names, 'Oscar', function(result){ - if(result === true){ - console.log('Oscar is in the array'); - } else { - console.log('Oscar is not in the array'); - } +contains(names, 'Oscar', function(result) { + if (result === true) { + console.log('Oscar is in the array'); + } else { + console.log('Oscar is not in the array'); + } }); // Do not edit the code above. @@ -108,8 +112,8 @@ contains(names, 'Oscar', function(result){ //Code Here // Do not edit the code below. -uniq(names, function(uniqArr){ - console.log('The new names array with all the duplicate items removed is ', uniqArr); +uniq(names, function(uniqArr) { + console.log('The new names array with all the duplicate items removed is ', uniqArr); }); // Do not edit the code above. @@ -125,8 +129,8 @@ uniq(names, function(uniqArr){ //Code Here // Do not edit the code below. -each(names, function(item, indice){ - console.log('The item in the ' + indice + ' position is ' + item) +each(names, function(item, indice) { + console.log('The item in the ' + indice + ' position is ' + item) }); // Do not edit the code above. @@ -142,28 +146,27 @@ each(names, function(item, indice){ // Code here // Do not edit the code below. -var users = [ - { - id: '12d', - email: 'aodhan@boom.camp', - name: 'Aodhan', - address: '167 East 500 North' - }, - { - id: '15a', - email: 'greg@boom.camp', - name: 'Greg', - address: '135 East 320 North' - }, - { - id: '16t', - email: 'Oscar@boom.camp', - name: 'Oscar', - address: '192 East 32 North' - }, +var users = [{ + id: '12d', + email: 'aodhan@boom.camp', + name: 'Aodhan', + address: '167 East 500 North' + }, + { + id: '15a', + email: 'greg@boom.camp', + name: 'Greg', + address: '135 East 320 North' + }, + { + id: '16t', + email: 'Oscar@boom.camp', + name: 'Oscar', + address: '192 East 32 North' + }, ]; -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); +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..04e3ca5 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" -} + "name": "Rolando Koji E Adriano Jr", + "email": "rolando.adriano@boom.camp" +} \ No newline at end of file From 5cff7bbeef4c7cb855cbbcf8ea6928eed75834c8 Mon Sep 17 00:00:00 2001 From: Koji Adriano Jr Date: Tue, 28 May 2019 14:30:21 +0800 Subject: [PATCH 2/4] 3-4 solved --- practice.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/practice.js b/practice.js index 161b32f..6c26333 100644 --- a/practice.js +++ b/practice.js @@ -69,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(n1, n2, callback) { + callback(n1 * n2) +} // Do not edit the code below. multiply(4, 3, function(answer) { @@ -88,7 +90,12 @@ multiply(4, 3, function(answer) { If the name does not exist, invoke the callback with false as the argument. */ -//Code Here +function contains(array, name, callback) { + array.forEach(function(n) { + callback(name === n) + }); +} + // Do not edit the code below. contains(names, 'Oscar', function(result) { From 78305473e255895f5f2a66eb5505aa2d97d740f7 Mon Sep 17 00:00:00 2001 From: Koji Adriano Jr Date: Tue, 28 May 2019 15:20:13 +0800 Subject: [PATCH 3/4] prob 6 checkpoint --- practice.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/practice.js b/practice.js index 6c26333..8a8ffb4 100644 --- a/practice.js +++ b/practice.js @@ -92,8 +92,9 @@ multiply(4, 3, function(answer) { function contains(array, name, callback) { array.forEach(function(n) { - callback(name === n) + name === n ? callback(true) : callback(false); }); + } @@ -116,7 +117,21 @@ 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(array, callback) { + console.log(array) + for (i = 0; i < array.length; i++) { + for (j = 0; j < array.length; j++) { + if (i != j) { + if (array[i] === array[j]) { + array.splice(i, 1); + } + } + } + } + callback(array); +} // Do not edit the code below. uniq(names, function(uniqArr) { @@ -133,7 +148,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(array, callback) { + array.forEach(function(name, index) { + callback(name, index) + }) +} // Do not edit the code below. each(names, function(item, indice) { From f692cb13343ddf2deb526c4e47abf9cdfcfb3c32 Mon Sep 17 00:00:00 2001 From: Koji Adriano Jr Date: Tue, 28 May 2019 15:29:14 +0800 Subject: [PATCH 4/4] callback done --- practice.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/practice.js b/practice.js index 8a8ffb4..4304fc5 100644 --- a/practice.js +++ b/practice.js @@ -169,7 +169,13 @@ 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(users, id, callback) { + users.forEach(function(n) { + if (n.id === id) { + callback(n); + } + }) +} // Do not edit the code below. var users = [{