Send a link to your students to track their progress
109 Terms
1
New cards
set up VS Code
1. install node 2. terminal: “node —version” “npm —version” 3. install on VS code: “javascript ES6” “code runner” 4. change to run code on terminal by searching in the setting of VS code 5. write on VS code terminal: “npm install prompt-sync”
2
New cards
```javascript
Hello World
```
add every time
3
New cards
//yay
comment
4
New cards
/\* yay \*/
multi line comment
5
New cards
console.log(“yay”);
“to print stuff”, no quotation marks for numbers
6
New cards
to embed JS to html
7
New cards
let name;
creates variable
8
New cards
name = ‘james’;
give value to variable
9
New cards
let x = 8;
let y = 54;
console.log(x+y);
print math equations
10
New cards
let x = (6+3)/5;
create variables from math equations
11
New cards
const color = ‘red’;
consonant. unlike variables, consonants usually dont change value. usually make uppercase to emphasize it shouldnt change.
12
New cards
const temperature = 30+2;
number consonant. can use either in "" to make string number or just regular number to add later to it.
13
New cards
document.write(x);
unlike console.log, write in document puts it in html
14
New cards
number = 3;
count++;
adds 1 to number
15
New cards
number = 3;
count += 2
adds 2 to number and outputs result
16
New cards
var course = {name: "JS", lessons: 41}; document.write(course.name.length);
round decimals. rounds 9.5564 to 9.60 and makes total price with $ sign. good for shopping sites
24
New cards
var prettyTotal = total.toFixed();
no decimals. just leave parentheses empty.
25
New cards
var month = readLine()
if (month == "August") {
console.log("vacation");
} else if ( month == "September") {
console.log("september");
} else {
console.log("not vacation"); }
if, else with words
26
New cards
var score = parseInt(readLine(), 10)
if (score>87) {
console.log("excellent");
} else if (score > 40 && score < 88) {
console.log("good");
} else {
console.log("fail"); }
if, else with numbers
27
New cards
if (dayOfWeek === "Sat" || dayOfWeek === "Sun") {
alert("Whoopee!");
} else if (dayOfWeek === "Fri") {
alert("TGIF!");
} else {
alert("Shoot me now"); }
if, else with alert
28
New cards
let answer = "Picasso";
if (answer === "Picasso") {
console.log (answer + " is correct!"); }
trivia
29
New cards
switch(dayOfWk){
case "Sat": alert("Whoopee");
break;
case "Sun": alert("Whoopee");
break;
case "Fri": alert("TGIF!");
break;
default: alert("Shoot me now!");
switch statement (similar to else if) with words and alert
30
New cards
var day = 2;
switch (day) {
case 1: console.log("Monday");
break;
case 2: console.log("Tuesday");
break;
case 3: console.log("Wednesday");
break;
default: console.log("Another day"); }
switch statement (similar to else if) with numbers and console.log
31
New cards
const subscribed = true
console.log("auto update")
console.log(false)
\
boolean. yes subscribed but not auto update
32
New cards
console.log(!true)
boolean opposite ! turns into opposite
33
New cards
const votes = 4
console.log(votes === 5);
console.log(votes !== 5); //unequal
compare if 2 are equal, get boolean result
34
New cards
var newText = text.replace("yay", "oh no");
replace new
35
New cards
var text = text.replace("yay", "oh no");
replace original
36
New cards
var newText = text.replace(/yay/g, "oh no");
global replace (replace all in the text)
37
New cards
const prompt = require('prompt-sync')();
var expression = prompt("write here: ");
var lowerCaseText = expression.toLowerCase();
console.log(lowerCaseText);
get user input and turn into lower case on vs code (readLine doesn’t work on web)
38
New cards
website: get user input and turn into lower case (readLine doesn’t work on web)
39
New cards
var person = { name: "John", age: 31, favColor: "green", height: 183 };
object with list of variables
40
New cards
var x = person.age; //method 1
var x = person\['age'\]; //method 2
document.write(x);
access variable from object
41
New cards
function person(name, age, color) {
this.name = name;
this.age = age;
this.favColor = color; }
object constructer (more complicated object)
42
New cards
var p1 = new person("John", 42, "green");
var p2 = new person("Amy", 21, "red");
document.write(p1.age);
document.write(p2.name);
once you have an object constructor, you can use the new keyword to create new objects of the same type. "this" means that we're referring to sth in this object
43
New cards
function person(name, age) {
this.name = name;
this.age = age;
this.lastName = last name;
method - function inside object. "this" means that we're referring to sth in this object
snail in the well - climbs 7 goes back 2, how many days will it take to climb “depth”
53
New cards
while (condition) {
run code
//example:
let i=0;
while(i
while loop
54
New cards
while (true) {
document.log(true); }
boolean condition while loop
55
New cards
var x=2
do {
console.log(x + " ");
x++; }
while (x
do while loop
56
New cards
let count = 0; count++; // or count += 1
console.log(count);
game. when need to repeatedly add 1 like attempts at a game
57
New cards
let score = 0;
score—;
game. subtracts repeatedly
58
New cards
if (health < 1) {
console.log(“game over”); }
else if (health >= 1 && health
game. health status in game
59
New cards
let colors = \[‘red’, ‘pink’, ‘blue’\]
//or
var new colors = \[‘red’, ‘pink’, ‘blue’\]
array. make new array
60
New cards
console.log(colors\[2\]);
array. access variable from array. 3rd one in this example
61
New cards
color\[2\] = ‘green’;
array. change 3rd variable to green
62
New cards
colors.pop(1);
array. remove 2nd variable from array
63
New cards
colors.push("yellow");
array. adds variable to end
64
New cards
colors.shift();
removes 1st variable
65
New cards
colors.unshift("black");
array. adds variable to beginning
66
New cards
var colorArrays = colors.concat(colors2);
array. connects arrays. adds colors2 to colors array
67
New cards
var courses = new Array(3);
courses\[0\] = "HTML";
courses\[1\] = "CSS";
courses\[2\] = "JS";
console.log(courses\[2\]);
array. declare an array, tell it the number of variables it will have and add to it
68
New cards
cityToCheck = cityToCheck.toLowerCase();
array. going through an array. check from input. problem is user might enter in lower or uppercase. we code in lowercase and convert input to lowercase
69
New cards
function name_of_function() {
//code }
name_of_function();
function. 2 first lines to make function, last line to call it later
70
New cards
function login (user) {
console.log(“hi” + user); }
login(“James”);
function. make a login function and call it
71
New cards
function add(x, y) {
return x+y; }
add (1, 2);
return statement
72
New cards
function main() {
var eventExample = readLine();
// function call
setReminder(eventExample)
console.log("You set a reminder about " + eventExample) }
function setReminder() { };
function. set reminder
73
New cards
functionName(param1, param2, param3) {
// some code }
function parameters
74
New cards
function sayHello(name, age) {
document.write( name + " is " + age + " years old."); }
function. make sentence
75
New cards
function tellTime() {
var now = new Date();
var theHour = now.getHours();
var theMin = now.getMinutes();
alert("Current time: " + theHr + theMin); }
function. tell time
76
New cards
function greetUser(greeting) {
alert(greeting); }
function. greet user, can add any greeting we want
77
New cards
function main() {
var goalsTeam1 = parseInt(readLine(), 10);
var goalsTeam2 = parseInt(readLine(), 10);
// function call
finalResult(goalsTeam1, goalsTeam2)
if(goalsTeam1>goalsTeam2) {
console.log("Team 1 won"); }
if(goalsTeam2>goalsTeam1) {
console.log("Team 2 won"); }
if(goalsTeam2==goalsTeam1) {
console.log("Draw"); } } function finalResult() { };
function. find winner
78
New cards
function main() {
var num1 = parseInt(readLine(),10);
var num2 = parseInt(readLine(),10);
var num3 = parseInt(readLine(),10);
var average = avg(num1,num2,num3);
console.log(average) }
function avg(x,y,z){
return (x+y+z)/3 }
function. find average
79
New cards
function main() {
var amount = parseFloat(readLine(), 10);
var rate = parseFloat(readLine(), 10);
function convert (a,b){
var c = a \* b;
return c }
console.log(convert(amount, rate)); }
function. currency converter
80
New cards
function main() {
//take flight number and its status
var flightNumber = readLine();
var flightStatus = readLine();
var flight1 = new Flight(flightNumber, flightStatus)