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
54 changes: 50 additions & 4 deletions practice.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
*/

// Code Here
function first(arr, callback) {
callback(arr[0]);
}


// Do not edit the code below.
var names = ['Aodhan', 'Greg', 'Jake', 'Oscar', 'Aodhan', 'Tanner', 'Greg'];
Expand All @@ -48,6 +52,9 @@ first(names, function(firstName){
*/

//Code Here
function last(arr,callback) {
callback(arr[arr.length-1]);
}

// Do not edit the code below.
last(names, function(lastName){
Expand All @@ -66,6 +73,9 @@ last(names, function(lastName){
*/

//Code Here
function multiply(x, y, callback) {
callback(x*y);
}

// Do not edit the code below.
multiply(4, 3, function(answer){
Expand All @@ -86,6 +96,18 @@ multiply(4, 3, function(answer){

//Code Here

function contains(arr, name, callback) {
let isBool;
for(let x of arr) {
if(x == name) {
callback(true);
} else {
isBool = false;
}
}
callback(isBool);
}

// Do not edit the code below.
contains(names, 'Oscar', function(result){
if(result === true){
Expand All @@ -102,10 +124,23 @@ contains(names, 'Oscar', function(result){

/*
Write a function called uniq that takes in an array and a callback function.
Remove any duplicate values from the array, and invoke the callback with the modified array as an argument.
Remove any duplicate values from the array, and invoke the callback with the modified array as an
argument.
*/

//Code Here
function uniq(arr, callback) {
for(let x in arr) {
for(let y in arr) {
if(y !== x) {
if(arr[x] == arr[y]) {
arr.splice(y,1);
}
}
}
}
callback(arr);
}

// Do not edit the code below.
uniq(names, function(uniqArr){
Expand All @@ -123,7 +158,11 @@ uniq(names, function(uniqArr){
*/

//Code Here

function each(arrName, callback) {
for(let x in arrName) {
callback(arrName[x], parseInt(x));
}
}
// Do not edit the code below.
each(names, function(item, indice){
console.log('The item in the ' + indice + ' position is ' + item)
Expand All @@ -135,12 +174,19 @@ each(names, function(item, indice){
////////// 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.
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(arrUser, id, callback) {
for(let x of arrUser) {
if(x.id === id) {
callback(x);
}
}
}
// 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": "Jaako Andes",
"email": "jaako.andes@boom.camp"
}