Day 97/100 Donuts to Code

riocantre

Rio Cantre

Posted on December 21, 2021

Day 97/100 Donuts to Code

banner

Intro to Arrays

An array is a data structure that you can use to store multiple values and arrays are also organized.

An array is useful because it stores multiple values into a single, organized data structure.

You can define a new array by listing values separated with commas between square brackets[].

var donuts = ["glazed", "jelly" , "powdered"];
Enter fullscreen mode Exit fullscreen mode

But strings aren't the only type of data you can store in an array. You can also store numbers, booleans... and really anything!

var mixedData = ["abcd", 1, true, undefined, null, "all the things"]; 
Enter fullscreen mode Exit fullscreen mode

You can even store an array in an array to create a nested array!

var arraysInArrays = [[1, 2, 3], ["Julia", "James"], [true, false, true, false]];
Enter fullscreen mode Exit fullscreen mode

Nested arrays can be particularly hard to read, so it's common to write them on one line, using a newline after each comma:

var arraysInArrays = [
    [1, 2, 3], 
    ["Julia", "James"], 
    [true, false, true, false]
]; 
Enter fullscreen mode Exit fullscreen mode

Indexing

Remember that elements in an array are indexed starting at the position 0. To access an element in an array, use the name of the array immediately followed by square brackets containing the index of the value you want to access.

var donuts = ["glazed", "powdered", "sprinkled"];

console.log(donuts[0]); // "glazed" is the first element in the `donuts` array
Enter fullscreen mode Exit fullscreen mode

Pop

Alternatively, you can use the pop() method to remove elements from the end of an array.

var donuts = ["glazed", "chocolate frosted", "Boston creme", "glazed cruller", "cinnamon sugar", "sprinkled", "powdered"];

donuts.pop(); // pops "powdered" off the end of the `donuts` array
donuts.pop(); // pops "sprinkled" off the end of the `donuts` array
donuts.pop(); // pops "cinnamon sugar" off the end of the `donuts` array
Enter fullscreen mode Exit fullscreen mode

With the pop() method you don’t need to pass a value; instead, pop() will always remove the last element from the end of the array.

Also, pop() returns the element that has been removed in case you need to use it.

var donuts = ["glazed", "chocolate frosted", "Boston creme", "glazed cruller", "cinnamon sugar", "sprinkled", "powdered"];

donuts.pop(); // the `pop()` method returns "powdered" because "powdered" was the last element on the end of `donuts` array
Enter fullscreen mode Exit fullscreen mode

Code Snippets

var donuts = ["jelly donut", "chocolate donut", "glazed donut"];

donuts.forEach(function(donut) {
     donut += " hole";
     donut = donut.toUpperCase();
     console.log(donut);
}); 


for (var i = 0; i < donuts.length; i++) {
     donuts[i] += " hole";
     donuts[i] = donuts[i].toUpperCase();
     console.log(donuts[i]);
}
Enter fullscreen mode Exit fullscreen mode

Summary

Received a meaningful comment and it is a great tool of motivation. I'm grateful for the people who are there supporting me.

resources

💖 💪 🙅 🚩
riocantre
Rio Cantre

Posted on December 21, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Day 100/100 Reverse Function
100daysofcode Day 100/100 Reverse Function

December 24, 2021

Day 99/100 Scope
100daysofcode Day 99/100 Scope

December 23, 2021

Day 98/100 Objects in Code
100daysofcode Day 98/100 Objects in Code

December 22, 2021

Day 97/100 Donuts to Code
100daysofcode Day 97/100 Donuts to Code

December 21, 2021

Day 96/100 Data Types
100daysofcode Day 96/100 Data Types

December 20, 2021