CS133JS Beginning Programming: JavaScript
| Topics by Week | |
|---|---|
| 1. Intro to JavaScript programming | 6. Arrays |
| 2. Functions, Operators and Expressions | 7. Objects and Object Constructors |
| 3. Conditional Statements: if and switch | 8. Web Page I/O |
| 4. Repetition: while, do while, and for | 9. Review / Continue Learning |
| 5. Midterm Check-Point | 10. Final Assessment |
AnnouncementsQ and ACombined StatementsCombined Statements with ArraysCombined Statements with ObjectsCombined Statements with the DOM
Any late or extra credit assignments are due by the end of the day on Wednesday of next week (finals week).
CS 135M, Mobile App Development is being offered next term (Winter term). You'll learn to make apps for iOS and Android using JavaScript.
How is lab 8 going?
Only part 1 is required. Is anyone doing part 2?
No code review required for part 1, production version due Thursday.
How is the term project going? (Dates are for fall 2024)
Beta version: send it to your lab partner Thursday, 12/5.
Code review for your lab partner due Friday, 12/6.
Production version due next Tuesday, 12/6.
Final quiz
There is a, short, ungraded practice quiz open now.
Look at the review suggestions in this week's overview.
The final quiz is at the regular class time next week. Or, take it in the Instructional Testing Center, Monday through Wednesday next week. See their web site to for the times they are open.
Any questions?
In JavaScript, as in most programming languages you can combine multiple operations into one statement—meaning you can do multiple things in one line of code. For example, here are a series of JavaScript statements to square a number entered by a user:
xxxxxxxxxxlet aNumber = 0; // declare and initialize a variableaNumber = prompt("Enter a number"); // get inputlet numberSquared = aNumber * aNumber; // square itconsole.log(numberSquared); // output the resultWe can combine some of the operations and still do exactly the same thing:
xxxxxxxxxxlet aNumber = prompt("Enter a number"); // declare a variable, get inputconsole.log(aNumber * aNumber); // square it, output the result
The possibility for combining operations gets more "interesting" (complicated?) when we are using arrays. For example, we'll make an array of animals and then output all the animals:
xxxxxxxxxxlet farmAnimals = []; // declare an arraylet animal1 = "cow"; // declare and initialze a variable with a valuefarmAnimals.push(animal1); // add an element to the array with the variable's valuelet animal2 = "chicken"; farmAnimals.push(animal2);let animal3 = "horse";farmAnimals.push(animal3);
for (let i = 0; i < farmAnimals.length; i++) { // loop through the index numbers let animal = farmAnimals[i]; // get the value from an elemnet console.log(animal); // output the value}We can do exactly the same thing like this:
xxxxxxxxxxlet farmAnimals = ["cow", "chicken", "horse"]; // declare and initializefor(let animal of farmAnimals) { // loop through the animals console.log(animal); // output the value}Here's another example, the Olympic Trials qualifying speed for the women's 100m dash is 11.15 seconds. We'll search an array of times for the runners who qualify:
xxxxxxxxxxconst QUALIFYING_TIME = 11.15;// parallel arrayslet times = [10.35, 12.92, 9.87, 11.16,12.01, 11.15]; // times in secondslet runners = ["Emily", "Hannah", "Madison", "Ashley", "Sarah","Alexis"]; // runnersfor(let i = 0; i < times.length; i++) { // loop through the array using it's index let time = times[i]; // copy array elements into variables let runner = runners[i]; if (time <= QUALIFYING_TIME) { // check for a qualifying time console.log(runner); // output the name of the qualifying runner }}We can simplify this by directly working with the array elements instead of copying them into variables:
xxxxxxxxxxconst QUALIFYING_TIME = 11.15;// parallel arrays--runners and times are at the same indecieslet times = [10.35, 12.92, 9.87, 11.16,12.01, 11.15]; // times in secondslet runners = ["Emily", "Hannah", "Madison", "Ashley", "Sarah","Alexis"]; // runnersfor(let i = 0; i < times.length; i++) { if (times[i] <= QUALIFYING_TIME) { console.log(runners[i]); }}
Objects provide even more opportunities for combining operations. For example we could combine the parallel arrays containing runners and their times into one array that contains runner objects:
xxxxxxxxxxconst QUALIFYING_TIME = 11.15;// Array of runner objects with name and time propertieslet runners = [ {name:"Emily", time:10.35}, {name:"Hannah", time:12.92}, {name:"Madison", time:9.87}, {name:"Ashley", time:11.16}, {name:"Sarah", time:12.01},{name:"Alexis", time:11.15}];
// Find the qualifying runners and display them on the consolefor(let i = 0; i < runners.length; i++) { let runner = runners[i]; // copy the runner object at element i to a variable let name = runner.name; // copy the runner's name to a variable let time = runner.time; // copy the runner's time to a variable if (time <= QUALIFYING_TIME) { console.log(name); }}We can simply the code in the loop by combining operations to directly operate on objects in array elements:
xxxxxxxxxx// Find the qualifying runners and display them on the consolefor(let i = 0; i < runners.length; i++) { if (runners[i].time <= QUALIFYING_TIME) { console.log(runners[i].name); }}We can simplify this loop further by using a for-of loop:
xxxxxxxxxx// Find the qualifying runners and display them on the consolefor(let runner of runners) { if (runner.time <= QUALIFYING_TIME) { console.log(runner.name); }}Since the objects in the Document Object Model are just JavaScript objects, we can combine multiple statements the operate on the DOM into one statement in the same way we would when working with any other JavaScript objects. Here's an example of setting the text content of an HTML element:
xxxxxxxxxx<p id="message"></p><script> let message = "Alone, we can do so little; together we can do so much. -- Helen Keller" let paragraph = document.getElementById("message"); paragraph.innerHTML = message;</script>We can combine this all into one statement:
xxxxxxxxxx<p id="message"></p><script> document.getElementById("message").innerHTML = "Alone, we can do so little; together we can do so much. -- Helen Keller";</script>
Beginning JavaScript Lecture Notes by Brian Bird, , revised in are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.