From 82f1906dcc687cde69522691f12c331753aa9b20 Mon Sep 17 00:00:00 2001 From: romeoaaron21 Date: Tue, 28 May 2019 13:51:45 +0800 Subject: [PATCH] Javascript-4-callbacks Completed --- practice.js | 84 ++++++++++++++++++++++------------------------------- user.json | 4 +-- 2 files changed, 37 insertions(+), 51 deletions(-) diff --git a/practice.js b/practice.js index a9e0f69..16110ab 100644 --- a/practice.js +++ b/practice.js @@ -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){ @@ -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 = [ { @@ -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. \ No newline at end of file diff --git a/user.json b/user.json index 4ac80a0..b1a41ce 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" + "name": "Romeo Aaron C. Lumibao I", + "email": "romeo.lumibao@boom.camp" }