Free code camp: Javascrip algorithms and Data structures - onika123/functional-programming GitHub Wiki
Notities bij demo-strings
Check met .indexOf of er een bepaald element in een array aanwezig is
function quickCheck(arr, elem) {
if(arr.indexOf(elem) >= 0) {
return true
}
else return false
}
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
Using a for loop, this function iterates through and accesses each element of the array, and subjects it to a simple test that we have created. In this way, we have easily and programmatically determined which data items are greater than 10, and returned a new array containing those items.
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for (let i = 0; i < arr.length; i++) {
if (arr[i].indexOf(elem) == -1){
//Checks every parameter for the element and if is NOT there continues the code
newArr.push(arr[i]); //Inserts the element of the array in the new filtered array
}
}
// Only change code above this line
return newArr;
}
console.log(filteredArray([3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9](/onika123/functional-programming/wiki/3,-2,-3],-[1,-6,-3],-[3,-13,-26],-[19,-3,-9), 3));
Opdracht: Somewhere on the third level, include the string 'deep', on the fourth level, include the string 'deeper', and on the fifth level, include the string 'deepest'.
let myNestedArray = [
// Only change code below this line
['unshift', false, 1, 2, 3, 'complex', 'nested'],
['loop', 'shift', 6, 7, 1000, 'method'],
['concat', false, true, 'spread', 'array'],
['mutate', 1327.98, 'splice', 'slice', 'push'],
['iterate', 1.3849, 7, '8.4876', 'arbitrary', 'depth']
// Only change code above this line
];
First trial:
console.log(myNestedArray[2])
-> (5) ["concat", false, true, "spread", "array"]
console.log(myNestedArray[2][0])
-> concat
let myNestedArray = [
// Only change code below this line
['unshift', false, 1, 2, 3, 'complex', 'nested'],
['loop', 'shift', 6, 7, 1000, 'method'],
['concat', false, true, 'spread', 'array',['deep']],
['mutate', 1327.98, 'splice', 'slice', 'push',
['deeper'](/onika123/functional-programming/wiki/'deeper')],
['iterate', 1.3849, 7, '8.4876', 'arbitrary', 'depth', [['deepest'](/onika123/functional-programming/wiki/['deepest')]],
// Only change code above this line
];
Opdracht: Using the syntax of your choice, add three more entries to it
let foods = {
apples: 25,
oranges: 32,
plums: 28
};
// Only change code below this line
foods.bananas = 13
foods.grapes = 35
foods['strawberries'] = 27
// Only change code above this line
console.log(foods);
userActivity has three properties: id (value is a number), date (value is a string), and data (value is an object with its nested structure). Opdracht: Set the value of the online key to 45.
let userActivity = {
id: 23894201352,
date: 'January 1, 2017',
data: {
totalUsers: 51,
online: 42
}
};
// Only change code below this line
userActivity.data.online = 45
// Only change code above this line
console.log(userActivity);
Opdracht: Return the current value of the scannedItem key in the foods object.
let foods = {
apples: 25,
oranges: 32,
plums: 28,
bananas: 13,
grapes: 35,
strawberries: 27
};
function checkInventory(scannedItem) {
// Only change code below this line
return foods[scannedItem]
// Only change code above this line
}
console.log(checkInventory("apples"));
Opdracht: Use the delete keyword to remove the oranges, plums, and strawberries keys from the foods object.
let foods = {
apples: 25,
oranges: 32,
plums: 28,
bananas: 13,
grapes: 35,
strawberries: 27
};
// Only change code below this line
delete foods.oranges;
delete foods.plums;
delete foods.strawberries;
// Only change code above this line
console.log(foods);
JavaScript provides us with two different ways to know if an object has a specific property. One uses the hasOwnProperty() method and the other uses the in keyword. Opdracht: Finish writing this function so that it returns true only if the users object contains all four names, Alan, Jeff, Sarah, and Ryan, as keys, and false otherwise.
let users = {
Alan: {
age: 27,
online: true
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: true
},
Ryan: {
age: 19,
online: true
}
};
function isEveryoneHere(obj) {
// Only change code below this line
if (obj.hasOwnProperty("Alan") &&
obj.hasOwnProperty("Jeff") &&
obj.hasOwnProperty("Sarah") &&
obj.hasOwnProperty("Ryan")
){
return true;
} else; {
return false
}
// Only change code above this line
}
console.log(isEveryoneHere(users));
Opdracht: Use a for...in statement within this function to loop through the users object passed into the function and return the number of users whose online property is set to true.
function countOnline(usersObj) {
// Only change code below this line
let result = 0;
for (let user in usersObj) {
if (usersObj[user].online === true ) {
result++;
}
}
return result
// Only change code above this line
}
Generate an array which contains all the keys stored in an object using the Object.keys()
Opdracht: Finish writing the getArrayOfUsers function so that it returns an array containing all the properties in the object it receives as an argument.
let users = {
Alan: {
age: 27,
online: false
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: false
},
Ryan: {
age: 19,
online: true
}
};
function getArrayOfUsers(obj) {
// Only change code below this line
return Object.keys(obj)
// Only change code above this line
}
console.log(getArrayOfUsers(users));
Opdracht: Finish writing it so that it takes a user object and adds the name of the friend argument to the array stored in user.data.friends and returns that array.
let user = {
name: 'Kenneth',
age: 28,
data: {
username: 'kennethCodesAllDay',
joinDate: 'March 26, 2016',
organization: 'freeCodeCamp',
friends: [
'Sam',
'Kira',
'Tomo'
],
location: {
city: 'San Francisco',
state: 'CA',
country: 'USA'
}
}
};
function addFriend(userObj, friend) {
// Only change code below this line
userObj.data.friends.push(friend)
return userObj.data.friends
// Only change code above this line
}
console.log(addFriend(user, 'Pete'));