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
84 changes: 35 additions & 49 deletions practice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,90 +2,79 @@
Once you complete a problem, refresh ./SpecRunner.html in your browser and check to see if the problem's test(s) are passing.
Passed tests will be indicated by a green circle.
Failed tests will be indicated by a red X.

You can refresh the page at any time to re-run all the tests.

In this repo your job is to write functions to make each function call work properly.

Here's an example of code that will be given to you:

sayHi('Hi Katie', function(thingToSay){
alert(thingToSay);
});

It would be your job to create the sayHi function:

var sayHi = function(str, cb){
cb(str);
}
*/

////////// PROBLEM 1 //////////

/*
Write a function called first that takes in two parameters, an array and a callback function.
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;
});
// Do not edit the code above.



////////// PROBLEM 2 //////////

/*
Write a function called last that takes in an array and a callback function.
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;
});
// Do not edit the code above.



////////// PROBLEM 3 //////////

/*
Write a function called multiply that takes in three parameters: two numbers and a callback function.
Invoke the callback, passing in the product of the two numbers multiplied as the argument.
*/

//Code Here

function multiply(num1, num2, callback){
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"
});
// Do not edit the code above.



////////// PROBLEM 4 //////////

/*
Write a function called contains that takes in three parameters: an array, a name and a callback.
Check if the name exists in the array.
If it does, invoke the callback with true as the argument.
If the name does not exist, invoke the callback with false as the argument.
*/

//Code Here

function contains(array, names, callback){
for(let i = 0; i <= array.length-1; i++){
if(names === array[i]){
callback(true);
} else{
callback(false);
}
}
}
// Do not edit the code below.
contains(names, 'Oscar', function(result){
if(result === true){
Expand All @@ -95,52 +84,50 @@ contains(names, 'Oscar', function(result){
}
});
// Do not edit the code above.



////////// PROBLEM 5 //////////

/*
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.
*/

//Code Here

function uniq(array, callback){
let unique = [...new Set(array)];
callback(unique);
}
// Do not edit the code below.
uniq(names, function(uniqArr){
console.log('The new names array with all the duplicate items removed is ', uniqArr);
});
// Do not edit the code above.



////////// PROBLEM 6 //////////

/*
Write a function called each that takes in an array of names and a callback function.
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(names, callback){
for(let i = 0; i <= names.length-1; i++){
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)
});
// 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(users, id, callback) {
for (var i = 0; i < users.length; i++) {
if (users[i]['id'] === id) {
callback(users[i]);
}
}
}
// Do not edit the code below.
var users = [
{
Expand All @@ -162,8 +149,7 @@ var users = [
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);
});
// Do not edit the code above.
// Do not edit the code above.
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": "Romeo Aaron C. Lumibao I",
"email": "romeo.lumibao@boom.camp"
}