CS 105

Week 3

MP1

Your first MP using JavaScript

Available on CS 105 Website
Due: Tomorrow, Feb. 10, 8:00pm

Activity 2

Write a function to return the square of a number.

Activity 2

var square = function(num) {
  var result = num * num;
  return result;
}

Activity 2

var square = function(num) {
  return num * num;
};

Activity 2

var square = function(num) {
  return (num * num);
};

Activity 2

var square = function(num) {
  // Does not work!
  return num ^ 2;
  
  // ^ does an "XOR",
  //   more on that in Week 13
};

Activity 2

Write a function that returns the maximum of two function parameters.

Activity 2

var max = function(a, b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
};

Activity 2

var max = function(a, b) {
  if (a > b) { return a; }
  else { return b; }
};

Activity 2

var max = function(a, b) {
  if (a >= b) { return a; }
  else { return b; }
};

Activity 2

var max = function(a, b) {
  if (a >= b) { return a; }
  if (a < b) { return b; }
};

Data Types

Numbers

If you are using a number (65, -32, 194) and want to do math on it, never use quotes.


Strings

If you are using anything that contains a letter, you must use quotes to represent it as a String.

Properties and Methods

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

Properties and 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

Strings

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

Strings

var s = "Hello";
console.log(s.length);

What is written to the console?

A) 1
B) 4
C) 5
D) s.length

Try it!

Strings

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

Strings

var s = "Hello";
console.log(s.charAt(1));

What is written to the console?

A) H
B) e
C) l
D) s.charAt(1)

Try it!

Strings

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.

Strings

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

Repeat Block

In Scratch, we would often repeat something many times using a repeat block.

Loops

In JavaScript, we can repeat with a for loop

for (var i = 0; i < ________; i++) {

}

for loops will always follow the same general syntax

Loops

for (var i = 0; i < 10; i++) {

}

Repeats 10 times

Loops

for (var i = 0; i < 555; i++) {

}

Repeats 555 times

Loops

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.

Loops

for (var i = 0; i < 20; i++) {
  console.log(i);
}

Try it!

Loops

var s = "Hello";
for (var i = 0; i < s.length; i++) {
  console.log(i);
}

What happens?

Loops with Strings

var s = "Illinois";
for (var i = 0; i < s.length; i++) {
  console.log(s.charAt(i));
}

Try it!