You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. Basic Data Structures: Use an Array to Store a Collection of Data
letyourArray=[1,2,3,true,false,"a","b","C"];// Change this line
2. Basic Data Structures: Access an Array's Contents Using Bracket Notation
letmyArray=["a","b","c","d"];// Only change code below this linemyArray[1]="not b";// Only change code above this lineconsole.log(myArray);
3. Basic Data Structures: Add Items to an Array with push() and unshift()
functionmixedNumbers(arr){// Only change code below this linearr.unshift('I',2,'three');arr.push(7,'VIII',9);// Only change code above this linereturnarr;}console.log(mixedNumbers(['IV',5,'six']));
4. Basic Data Structures: Remove Items from an Array with pop() and shift()
functionpopShift(arr){letpopped=arr.pop();// Change this lineletshifted=arr.shift();// Change this linereturn[shifted,popped];}console.log(popShift(['challenge','is','not','complete']));
5. Basic Data Structures: Remove Items Using splice()
constarr=[2,4,5,1,7,5,2,1];// Only change code below this linearr.splice(1,4);// Only change code above this lineconsole.log(arr);
6. Basic Data Structures: Add Items Using splice()
functionhtmlColorNames(arr){// Only change code below this linearr.splice(0,2,'DarkSalmon','BlanchedAlmond');// Only change code above this linereturnarr;}console.log(htmlColorNames(['DarkGoldenRod','WhiteSmoke','LavenderBlush','PaleTurquoise','FireBrick']));
7. Basic Data Structures: Copy Array Items Using slice()
functionforecast(arr){// Only change code below this linearr=arr.slice(2,4);returnarr;}// Only change code above this lineconsole.log(forecast(['cold','rainy','warm','sunny','cool','thunderstorms']));
8. Basic Data Structures: Copy an Array with the Spread Operator
functioncopyMachine(arr,num){letnewArr=[];while(num>=1){// Only change code below this linenewArr.push([...arr]);// Only change code above this linenum--;}returnnewArr;}console.log(copyMachine([true,false,true],2));
9. Basic Data Structures: Combine Arrays with the Spread Operator
functionspreadOut(){letfragment=['to','code'];letsentence=['learning', ...fragment,'is','fun'];// Change this linereturnsentence;}console.log(spreadOut());
10. Basic Data Structures: Check For The Presence of an Element With indexOf()
functionquickCheck(arr,elem){// Only change code below this linereturn(arr.indexOf(elem)!=-1);// Only change code above this line}console.log(quickCheck(['squash','onions','shallots'],'mushrooms'));
11. Basic Data Structures: Iterate Through All an Array's Items Using For Loops
functionfilteredArray(arr,elem){letnewArr=[];// Only change code below this linefor(leti=0;i<arr.length;i++){//Check elem does not exist in arrryif(arr[i].indexOf(elem)==-1){newArr.push(arr[i]);}}// Only change code above this linereturnnewArr;}console.log(filteredArray([[3,2,3],[1,6,3],[3,13,26],[19,3,9]],3));
12. Basic Data Structures: Create complex multi-dimensional arrays
13. Basic Data Structures: Add Key-Value Pairs to JavaScript Objects
letfoods={apples: 25,oranges: 32,plums: 28,};// Only change code below this linefoods.bananas=13;foods.grapes=35;foods.strawberries=27;// Only change code above this lineconsole.log(foods);
14. Basic Data Structures: Modify an Object Nested Within an Object
letuserActivity={id: 23894201352,date: 'January 1, 2017',data: {totalUsers: 51,online: 42}};// Only change code below this lineuserActivity.data.online=45;// Only change code above this lineconsole.log(userActivity);
15. Basic Data Structures: Access Property Names with Bracket Notation
letfoods={apples: 25,oranges: 32,plums: 28,bananas: 13,grapes: 35,strawberries: 27};functioncheckInventory(scannedItem){// Only change code below this linereturnfoods[scannedItem];// Only change code above this line}console.log(checkInventory("apples"));
16. Basic Data Structures: Use the delete Keyword to Remove Object Properties
letfoods={apples: 25,oranges: 32,plums: 28,bananas: 13,grapes: 35,strawberries: 27};// Only change code below this linedeletefoods.oranges;deletefoods.plums;deletefoods.strawberries;console.log(foods);
17. Basic Data Structures: Check if an Object has a Property
letusers={Alan: {age: 27,online: true},Jeff: {age: 32,online: true},Sarah: {age: 48,online: true},Ryan: {age: 19,online: true}};functionisEveryoneHere(obj){// Only change code below this linereturnobj.hasOwnProperty("Alan")&&obj.hasOwnProperty("Jeff")&&obj.hasOwnProperty("Sarah")&&obj.hasOwnProperty("Ryan")// Only change code above this line}console.log(isEveryoneHere(users));
18. Basic Data Structures: Iterate Through the Keys of an Object with a for...in Statement
functioncountOnline(usersObj){// Only change code below this lineletnumOfUser=0;for(letuserinusersObj){if(usersObj[user].online){numOfUser++;}}returnnumOfUser;// Only change code above this line}
19. Basic Data Structures: Generate an Array of All Object Keys with Object.keys()
letusers={Alan: {age: 27,online: false},Jeff: {age: 32,online: true},Sarah: {age: 48,online: false},Ryan: {age: 19,online: true}};functiongetArrayOfUsers(obj){// Only change code below this linereturnObject.keys(obj);// Only change code above this line}console.log(getArrayOfUsers(users));
20. Basic Data Structures: Modify an Array Stored in an Object
letuser={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'}}};functionaddFriend(userObj,friend){// Only change code below this lineuserObj.data.friends.push(friend);returnuserObj.data.friends;// Only change code above this line}console.log(addFriend(user,'Pete'));