diff --git a/practice.js b/practice.js index a9e0f69..9f7e1f8 100644 --- a/practice.js +++ b/practice.js @@ -27,7 +27,10 @@ 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']; @@ -47,7 +50,9 @@ 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){ @@ -65,7 +70,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, callback){ + callback(num1*num2) +} // Do not edit the code below. multiply(4, 3, function(answer){ @@ -84,7 +91,10 @@ 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){ + if (array.includes(name)) callback(true) + else callback(false) +} // Do not edit the code below. contains(names, 'Oscar', function(result){ @@ -105,7 +115,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(array, callback){ + + var uniqueArray = []; + for(i=0; i < array.length; i++){ + if(uniqueArray.indexOf(array[i]) === -1) { + uniqueArray.push(array[i]); + } + } + callback(uniqueArray); +} // Do not edit the code below. uniq(names, function(uniqArr){ @@ -122,7 +141,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){ + for (val of array){ + callback(val, array.indexOf(val)) + } +} // Do not edit the code below. each(names, function(item, indice){ @@ -139,7 +162,14 @@ 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){ + for (val in users) { + if (users[val].id === id) { + callback(users[val]); + } + } + +} // Do not edit the code below. var users = [ diff --git a/user.json b/user.json index 4ac80a0..c1982a8 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" + "name": "Lyza Mae B. Mirabete", + "email": "lyza.mirabete@boom.camp" }