JS

0.0(0)
studied byStudied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/108

flashcard set

Earn XP

Description and Tags

Last updated 5:01 AM on 8/1/23
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

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);
writes number of letters in the course name
17
New cards
var cuboid = {

length: 25,

width: 50,

height: 200 };

console.log(cuboid.length\*cuboid.width\*cuboid.height)
calculate volume
18
New cards
&&
and
19
New cards
||
or
20
New cards
var integerString = "24";

var num = Number(integerString);
convert string to number
21
New cards
var floatingNumString = "24.9876";

var num = Number(floatingNumString);
convert string to float
22
New cards
var numberAsNumber = 1234;

var numberAsString = numberAsNumber.toString();
convert answer to string
23
New cards
var total = price + (price \* taxRate);

var prettyTotal = total.toFixed(2);

var currencyTotal = "$" + pretty Total;
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
44
New cards
function person(name, age) { 

this.changeName = function (name) {

this.name = name; } //changes the name
change name of object name
45
New cards
function person(name, age) { 

fullName: function () {

document.write(this.name + " " + this.lastName); //way 1

return this.name + " " + this.lastName; } }; //way 2
write full name from person function with name
46
New cards
var user = {

firstName = "first",

lastName = "last",

role: "Admin",

loginCount = 32,

facebookSignedIn = true,

courseList: \[\],

buyCourse: function(courseName) {

this.courseList.push(courseName); },

getCourseCount: function () {

return '${this.firstName} is enrolled in ${this.courseList.length} courses'; }, };

user.buyCourse("course 1");

console.log(user.getCourseCount());
count courses enrolled. 2 last lines to add another course
47
New cards
for (initializer; condition; final-expression) {

if (i==3) {

break; } //break loop prematurely

if (i==4) {

continue; } //skips only this one and continues to next

console.log(i) }
for loop.

The initializer is a variable, which increments the number of times the loop has run.

The condition is used to stop the loop.

The final-expression is run each time, after the loop's code has run. It is usually used to increment the variable used in the condition. 
48
New cards
for (let i=1; i
for loop. ++ increases value by 1
49
New cards
for (let i=0; i
for loop. outputs only even numbers
50
New cards
var expression = readLine () ;

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

console.log (expression) }; 
log input expression 3 times. i++ to increase value each loop
51
New cards
var x = 0;

for (var x=0; x
print even values from 0 to 20
52
New cards
function main() {

var depth = parseInt(readLine(), 10);

for(dis = 7, day = 1; dis < depth; dis += 7, day++) {

dis -= 2 }

console.log(day); }
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)

//assign a flight object to flight1 variable

//output

console.log('The flight ' + flight1.number + ' is ' + flight1.status) }

function Flight(flightNumber, status) {

//fix the constructor

this.number = flightNumber;

this.status = status; };
function. flight status
81
New cards
function main() {

var prodID = readLine();

var price = parseInt(readLine(),10);

var discount = parseInt(readLine(),10);

var prod1= new Product(prodID, price);

console.log(prod1.prodID + " price: " + prod1.price);

prod1.changePrice(discount);

console.log(prod1.prodID + " new price: " + prod1.price); }

function Product(prodID, price) {

this.prodID = prodID;

this.price = price;

this.changePrice = function(discount) {

this.price = price - ((discount/100) \* price); } }
function. calculate discount
82
New cards
function person(name, age) {

this.name= name;

this.age = age;

this.yearOfBirth = bornYear; }

function bornYear() {

return 2016 - this.age; }
function. find birth year
83
New cards
function Contact(name, number) {

this.name = name;

this.number = number;

this.print = function () {

var x = this.name

var y = this.number

console.log(x+": "+y) } }

var a = new Contact("David", 12345);

var b = new Contact("Amy", 987654321)

a.print();

b.print();
function. a contact manager
84
New cards



window.onload = function() { let c = document.getElementById("celsius”); let f = document.getElementById("fahr"); c.oninput = function() { f.value = (c.value \* 9/5) + 32: }; f.oninput = function() { c.value = (f. value 32)\* 5/9; };
function. temperature converter
85
New cards
var rightNow = new Date();
built in function. date
86
New cards
Math
built in function. all math functions begin with the word “Math.” begins with capital M and rounds up 0.5 to 1
87
New cards
scoreAvg = Math.round(scoreAvg);

//or

scoreAvg = Math.round(0.678);
built in function. math function to round up
88
New cards
scoreAvg = Math.ceil(1.03); //rounds to 2
built in function. math function to round up to closest round number
89
New cards
scoreAvg = Math.floor(1.03); //rounds to 1
built in function. math function to round down to closest round number
90
New cards
alert("Do you really want to leave this page?");
alert.

1 parameter: the text diplayed
91
New cards
var user = prompt("Please enter your name");

alert(user);
prompt.

user needs to enter value then click ok
92
New cards
var result = confirm("Do you really want to leave this page?");

if (result == true) {

alert("Thanks for visiting");

} else {

alert("Thanks for staying with us"); }
confirm.

verify with ok or cancel
93
New cards
navigator.geolocation.getCurrentPosition();
web. get user location
94
New cards
sessionStorage() //destroyed once user exits page localStorage() //stores data no expiration
web. choose what to do with data
95
New cards
log JS onto browser like html
96
New cards
window.onload = function() { let button = document.getElementById(“login”); btn.onClick = hello; };
give html buttons function. say hello on login
97
New cards
give html buttons function. give toggle function to button like on website
98
New cards
getElementById

getElementByTagName
target certain html. good for things like changing background color or size on click
99
New cards
let counter1 = 0;

while (counter1 < 5) {

console.log("\*\*\*\*\*\*\*\*\*\*---------");

counter1++; }

let counter2 = 0;

while (counter2 < 4) {

console.log ("-------------------");

counter2++; }
while loop example. flag while
100
New cards
for (let i = 0; i < 5; i++) {

console.log ("\*\*\*\*\*\*\*\*\*\*---------");

for (let i = 0; i < 4; i++) {

console.log ("-----------------");
for loop example. flag for