Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions practice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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){
Expand All @@ -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){
Expand All @@ -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){
Expand All @@ -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){
Expand All @@ -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){
Expand All @@ -131,15 +156,19 @@ each(names, function(item, indice){
// Do not edit the code above.



////////// PROBLEM 7 //////////

/*
Write a function called getUserById that takes in three parameters: an array of objects (users), an id and a callback, and searches for the user with a matching id.
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 = [
Expand Down
4 changes: 2 additions & 2 deletions user.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "",
"email": ""
"name": "Jericho Aldemo",
"email": "jericho.aldemo@boom.camp"
}