Your first MP using JavaScript
Available on CS 105 Website
Due: Tomorrow, Feb. 10, 8:00pm
Write a function to return the square of a number.
var square = function(num) { var result = num * num; return result; }
var square = function(num) { return num * num; };
var square = function(num) { return (num * num); };
var square = function(num) { // Does not work! return num ^ 2; // ^ does an "XOR", // more on that in Week 13 };
Write a function that returns the maximum of two function parameters.
var max = function(a, b) { if (a > b) { return a; } else { return b; } };
var max = function(a, b) { if (a > b) { return a; } else { return b; } };
var max = function(a, b) { if (a >= b) { return a; } else { return b; } };
var max = function(a, b) { if (a >= b) { return a; } if (a < b) { return b; } };
If you are using a number (65, -32, 194) and want to do math on it, never use quotes.
If you are using anything that contains a letter, you must use quotes to represent it as a String.
In JavaScript, variables contain additional information and functionality about them inside of their properties.
Some properties are functions, in which case we call them methods
console.log("Hello");
The . (dot operator) asks the variable thing before the dot for a property
The code asks console for the property log
console.log is a function, so we call it with ()
When the property is a function, we call them by a special name called: a method
var s = "Hello";
The variable s contains a string
Strings have a .length property that tells us how many characters are in the string
.length is a Number, so you do NOT use ()s
.length is NOT a function, so we call it a property
var s = "Hello"; console.log(s.length);
What is written to the console?
var s = "Hello"; console.log(s.charAt(1));
The variable s contains a string
Strings have a .charAt method that returns the character at a specified index
We said .charAt is a method, so it must be a function
var s = "Hello"; console.log(s.charAt(1));
What is written to the console?
var s = "Hello"; console.log(s.charAt(1));
Strings have a .charAt method that returns the character at a specified index
Indexes always start at 0 and count up from 0.
var s = "Illinois"; console.log(s.length); // 8 console.log(s.charAt(0)); // "I" console.log(s.charAt(7)); // "s" console.log(s.charAt(7).length); // 1
In Scratch, we would often repeat something many times using a repeat block.
In JavaScript, we can repeat with a for loop
for (var i = 0; i < ________; i++) { }
for loops will always follow the same general syntax
for (var i = 0; i < 10; i++) { }
Repeats 10 times
for (var i = 0; i < 555; i++) { }
Repeats 555 times
for (var i = 0; i < 20; i++) { }
Each time i will start a 0 and go until one less than the number of times you ask it to repeat.
for (var i = 0; i < 20; i++) { console.log(i); }
var s = "Hello"; for (var i = 0; i < s.length; i++) { console.log(i); }
What happens?
var s = "Illinois"; for (var i = 0; i < s.length; i++) { console.log(s.charAt(i)); }