Homework: Arrays
Homework Create a new codepen called colorblocks. Make your body background be gray and your canvas white. Create an array with three different colors in hexadecimal. Print out a 100 by 100 pixel square of each of the three colors in a horizontal line. Add a fourth color and a fifth color to the array…
Read MoreJoining, Splitting & Converting Arrays
Note: this section is not needed for the homework – more like side info! Converting Arrays to String The JavaScript method toString() converts an array to a string of comma separated array values. var animals =[“monkey”, “panda”, “shark”, “cat”, “bunny”]; document.write(animals.toString()); Output monkey, panda, shark, cat, bunny The join() method does the same thing but…
Read MoreChanging Arrays
Adding or Removing Elements The easiest way to add elements to an array is to use the push method. kids.push(“ Kate ”); //kids is now [‘Teo’, ‘Cal’, ‘Zoe’, ‘Tessa’, ‘Kate’] You can also add an element using the length property. kids[kids.length] = “ Fiona ”; //kids is now [‘Teo’, ‘Cal’, ‘Zoe’, ‘Tessa’, ‘Kate’, ‘Fiona’] You…
Read MoreHomework: Converting Strings & Numbers
Your homework is to write a bit of code called convert.html. In that you will do both Example 1 & Example 2 from the topic! Here they are again: Example 1 You are calculating the cost of an ice cream based on how many scoops and toppings the person is asking for. Scoops are $1.00…
Read MoreString Methods in JavaScript
In JavaScript there are lots of ways to manipulate strings including: finding certain characters in a string, replacing characters, splitting a string or converting the case. Basic String Methods These are two of the most basic string methods. String Length: returns the length of the string or the number of characters in the string. var…
Read MoreHomework: First JavaScript Program
Create a new HTML file called jsfirst.html. Add a link to the file to your index.html. In the new file: Create your basic document structure – remember how? Create a header that says “JavaScript!!!”. Or something. Add a script that outputs a string to the screen that says “It’s JavaScript in the time of COVID.”…
Read MoreConverting Strings to Numbers & Vice Versa
Sometimes, you need a number to work like a string. And sometimes you need a string to act like a number. Why? Here are a couple of examples. Example 1 You are calculating the cost of an ice cream based on how many scoops and toppings the person is asking for. This cost will be…
Read MoreVariables
How to Create a Variable A variable stores a value. Use logical names so you’ll know what information you are keeping there! There are three basic types of actions involved in the creation of variables. Declaration This declares that a variable exists. You do not store any information in the variable. Yet. You use the…
Read MoreCreating Arrays in JavaScript
Creating an array To create an array: //this creates a blank array var kids = []; //this creates an array with string values in it var kids = [“Teo”,Cal”,Kate”,Zoe”]; To access the elements in an array– use the index number. You can store the element in a variable: var kid1 = kids[0]; Or you can…
Read MoreObjects in JavaScript
The for..in Loop If you have learned about objects in JavaScript -the For/in loop will loop through the properties of an object. var animal= {name:”ant”, type:”insect”, transportmode”crawl”}; var text = “”; var x; for (x in person) { text += person[x] + ” “; } document.write(text); Output: ant insect crawl
Read More