Week 6 - Saturday, May 22nd
Topics Covered
Thinking about how to solve problems with a computer.
Arrays, For loop
The code below
is the same as
In the second case, you have a lot more code, and it's just repeating the same thing, except you're replacing the index you are using to access the element in the array with a number one higher.
Also, in the second case, you don't have a way to know when to stop repeating the two lines of code to extract the number and print it out on the console. What if there are a billion numbers in the array? You wouldn't want to repeat the statements on lines 4-5 one billion times. And, you may not know ahead of time how many numbers are in the array, so it's not even possible to write the code ahead of time as we're doing above.
That's why there is a for loop.
It allows you to write the statements that you want to execute for each element a single time within the body of the for loop and it provides a mechanism to track what position your are currently at and when to stop.
Class Video
Homework
Turn in the assignment by either showing me your solution, or pasting in the code from the jscript exercises files into Canvas for the assignment jscript-hw-2.
Download and unzip the project file from the github repository.
I have included three JavaScript files in the index.html file. Each will be loaded and any statements in any of the three files will be evaluated.
You can open any of the exercise files and start working on it. If you want to prevent the code from one of the files from being evaluated while you are working on another exercise, then just comment out those lines in the html file.
For example, to comment out exercise1.js, you would
Exercise 1
The code for this exercise is in the source file exercise1.js
Exercise 2
The code for this exercise is in the source file exercise2.js
Exercise 3
The code for this exercise is in the source file exercise3.js.
Solutions
Exercise 1
Exercise 2
Step 1
Step 2
Exercise 3
Sorry, I picked a bad name for the array of words. I originally named it letters (not sure why). I renamed it words here. Hopefully, it now makes more sense.
Talked about in class
Last updated
Was this helpful?