Strings and numbers may be our building blocks but as your scripts get more complex you’re going to need a way to deal with large quantities of them. Luckily, JavaScript has a couple of data types that are used for just that. An Array is simply an ordered collection of values (Strings, Numbers, other Arrays, etc.).
Watch this short introductory video on Arrays by Code.org:
Duration: 4 minutes
Watch this recorded Live Session (04/11/2020) about String and Array Methods
Part 1/3 | Duration: 1 hour
Part 2/3 | Duration: 2 hours
Part 3/3 | Duration: 1 hour
The code and exercises for this live session can be found here: JavaScript: Array and String Methods.
Some things to be aware of while going through the tutorial:
var x = cars.length;
var y = cars.sort();
let carsLength = cars.length;
let sortedCars = cars.sort();
The same best practices mentioned above (avoid var
, use meaningful variable names) apply to this tutorial also.
You can play around with this codepen we created to get a better understanding of how the Array splice method works.
NOTES: If you are unaware of the following command mentioned in this tutorial, here’s a short explanation of what it does:
document.getElementById("demo").innerHTML = fruits;
This command searches through the document (our HTML page), for an element with id demo and replaces its HTML content with the contents of the fruits variable.
For example, let’s say we have the following HTML content in our document:
<div id="target">Some content here</div>
The following code will replace the current HTML content of this div (Some content here) with <strong>JS rocks!</strong>:
let content = "<strong>JS rocks!</strong>";
document.getElementById("target").innerHTML = content;
The getElementById command is part of the so-called DOM Manipulation API, a fancy word for the set of JavaScript commands available to us for manipulating our page’s HTML and CSS (adding elements, removing elements, creating elements, updating the elements’ content, and much much more). This is the true power of JS.
Computers don’t get tired, and they’re really really fast! For that reason they are well suited to solving problems that involve doing calculations multiple times. In some cases a computer will be able to repeat a task thousands or even millions of times in just a few short seconds where it might take a human many hours. (obviously speed here depends on the complexity of the calculation and the speed of the computer itself). One way to make a computer do a repetitive task is using a loop
Read this MDN article: Looping Code. It’s a long one, but make sure you tackle the ‘Active Learning’ sections at the bottom of the page.
Once again, same info, slightly different context from JavaScript.info (Skim the info if you think you know it all, but don’t forget the tasks at the end of the page. You learn best by doing)
Test Driven Development (TDD) is a phrase you often hear in the dev world. It refers to the practice of writing automated tests that describe how your code should work before you actually write the code. For example, if you want to write a function that adds a couple of numbers, you would first write a test that uses the function and supplies the expected output. Before you write your code the test will fail, and you should be able to know that your code works correctly when the tests pass.
In many ways TDD is much more productive than writing code without tests. If we didn’t have the test for the adding function above, we would have to run the code ourselves over and over, plugging in different numbers until we were sure that it was working… not a big deal for a simple add(2, 2)
, but imagine having to do that for more complicated functions, like checking whether or not someone has won a game of tic tac toe: (game_win(["o", null,"x",null,"x",null,"x", "o", "o"])
) If you didn’t do TDD then you might actually have to play multiple games against yourself just to test if the function was working correctly!
We will teach you the art of actually writing these tests later in the course. The following exercises have the tests already written out for you. All you have to do is read the specs and write the code that makes them pass! The very first exercise (01-helloWorld
) is intentionally very simple and walks you through the process of running the tests and making them pass.
Complete the following exercises found in the repository here and submit the Quiz.
UPDATED: 27.11.2020
Array.prototype.splice