Commit 3236e4ac authored by Michael Jörg's avatar Michael Jörg

Basic Data Structures

parent a34145da
let myArray = ["a", "b", "c", "d"];
// Only change code below this line
myArray[1] = "_";
// Only change code above this line
console.log(myArray);
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"));
function mixedNumbers(arr) {
// Only change code below this line
arr.unshift('three');
arr.unshift(2);
arr.unshift('I');
arr.push(7);
arr.push('VIII');
arr.push(9);
// Only change code above this line
return arr;
}
console.log(mixedNumbers(['IV', 5, 'six']));
function htmlColorNames(arr) {
// Only change code below this line
arr.splice(0, 2, "DarkSalmon", "BlanchedAlmond");
// Only change code above this line
return arr;
}
console.log(htmlColorNames(['DarkGoldenRod', 'WhiteSmoke', 'LavenderBlush', 'PaleTurquoise', 'FireBrick']));
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);
function quickCheck(arr, elem) {
// Only change code below this line
if (arr.indexOf(elem) >= 0) {
return true;
};
return false;
// Only change code above this line
}
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
console.log(quickCheck(['squash', 'onions', 'shallots'], 'onions'));
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
return obj.hasOwnProperty('Alan')
&& obj.hasOwnProperty('Jeff')
&& obj.hasOwnProperty('Sarah')
&& obj.hasOwnProperty('Ryan');
// Only change code above this line
}
console.log(isEveryoneHere(users));
function spreadOut() {
let fragment = ['to', 'code'];
let sentence = ['learning', ...fragment,'is', 'fun']; // Change this line
return sentence;
}
console.log(spreadOut());
function copyMachine(arr, num) {
let newArr = [];
while (num >= 1) {
// Only change code below this line
newArr.push([...arr]);
// Only change code above this line
num--;
}
return newArr;
}
console.log(copyMachine([true, false, true], 2));
function forecast(arr) {
// Only change code below this line
arr = arr.slice(2, 4);
return arr;
}
// Only change code above this line
console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));
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"]]],
["iterate", 1.3849, 7, "8.4876", "arbitrary", "depth", [[["deepest"]]]]
// Only change code above this line
];
console.dir(myNestedArray);
\ No newline at end of file
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));
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) {
newArr.push(arr[i]);
}
}
// Only change code above this line
return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
function countOnline(usersObj) {
// Only change code below this line
let count = 0;
for (let user in usersObj) {
console.log(user);
if (usersObj[user].online) {
console.log(user + " is online");
count++;
}
}
return count;
// Only change code above this line
}
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'));
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);
function popShift(arr) {
let popped = arr.pop(); // Change this line
let shifted = arr.shift(); // Change this line
return [shifted, popped];
}
console.log(popShift(['challenge', 'is', 'not', 'complete']));
const arr = [2, 4, 5, 1, 7, 5, 2, 1];
// Only change code below this line
arr.splice(1, 4);
// Only change code above this line
console.log(arr);
\ No newline at end of file
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);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment