Cost of Cart
Cost of Cart
alert(‘hello’);
1ORDER OF OPERATIONS
+ - * / ARE OPERATORS
ORDER OF OPERATION IS ALSO CALLED OPERATOR PRSIDENCE
* / are calculated 1st then + - are calculated 2nd
Add from left to right
( ) nUMBERS INSIDE THE BRACKETS are CALCULATED FIRST
% - CANNOT BE CALCULATE _*_%
1%= 1/100=0.01
WORK IN TEXT
2TYPE OF VALUES
1st "number"
typeof 'hello'
2nd "string"
'hello' + 3
"hello3" type of coercion
3STRING E.g
'Items ( ' + (1 + 1) + '): $' + ( 2095 + 799) /100 -concatenation(breaking down then adding
together)
"Items (2): $28.94"
TEMPLATE STRING
ESCAPE CHARACTERS
\’’
\n(newline character)
HTML(HYPER TEXT MARKUP LANGUAGE) - CREATE THE CONTENT OF THE WEBSITE E.g button
CSS(CASCADIND STYLE SHEETS)– CHANGES THE APPEARANCE
JAVASCRIPT – MAKES IT INTERACTIVE
1HTML ELEMENTS
TO CREATE A HTML ELEMENT U START < THEN ELEMENT NEME THEN >
E.g <button> - called HTML TAG
Opening tag -<button> closing tag - </button>
1.1CSS
<style></style> - doesn’t appear in the page but it let us write css code
<link rel="stylesheet" href="styles/10-rock-paper-scissor.css">
.css – file(hperlink)
CSS SYTRAX
Button called CSS SELECTOR(ELEMENT U WANT TO CHANGE)
-{}- inside CSS STTLE(HOW TO CHANGE THE APPEARANCE)
-LEFT SIDE CALLED CSS PROPERTY RIGHT SIDE IS CALLED VALUE
<style>
button{
background-color: red;
color: white;
border: none;
}
HTML ATTRIBUTE
.blue-button{
background-color: blue;
</style>
<button title="tooltip" class=" red-button">
hello
</button>
<p>
paragraph of
<button class= "blue-button">hello</button> text
HTML STRUCTURE
<!DOCTYPE html> - tells the browser to use a modern version of html for better features
<html></html> - represent an entire webpage
<head></head> - contain information about the page(not visible)
<body></body> - represent everything visible on the page
<!DOCTYPE html>
<html>
<head>
<style>
.red-button{
background-color: red;
color: white;
border: none;
}
.blue-button{
background-color: blue;
}
</style>
</head>
<body>
<button title="tooltip" class=" red-button">
hello
</button>
<p>
paragraph of
<button class= "blue-button">hello</button> text
</p>
</body>
</html>
COMMENTS
IS LIKE A CONTAINER YOU CAN A SAVE NUMBER OR STRING THEN USE IT LATER
let create a variable
Consol.log
let variable1 = 3;
console.log(variable1);
let calculation = 2 + 2;
console.log(calculation);
console.log(calculation + 2);
variable1 = variable1 + 1;
console.log(variable1);
<body>
<button onclick="
console.log(`cart quantity: ${cartQuantity}`); <!--template
string-->
">Show Quantity</button>
NAMING CONVENTION(camelCase)
COMBINING NAMES TOGETHER AND CAPATILIZE EVERY WORD EXCEPT THE 1 ST WORD e.g
cartQuantity
camelCase is the standard naming convection for javascript
PascalCase is another naming convention
kebab-case cannot work in javascript because the dash is used as a subtraction sign
We use kebab-case in html and css and file naming
Snake_case is not used in used in javascript
console.log(variable1);
variable1 = variable1 + 1;
console.log(variable1);
3rd way is to use var (variable)
Is the original way to create variable
We no longer use var in new javascript code because it has problems problems
Typeof
LETS US WRITE MULTIPLE GROUPS OF CODE AND THEN DECIDE WHICH CODE TO RUN
if (true) {
console.log('hello');
if true 'hello' appear in console, if false it doesn’t display
in console
if (true) {
console.log('hello');
} else {
console.log('else')
}
if true 'hello' appear in console
if false 'else'appear in console
} else {
console.log('you cannot drive');
else {
console.log('you cannot drive');
}
else {
console.log('you cannot drive');
}
FALSELY VALUE BEHAVE LIKE FALSE – FALSE, 0, ‘’ , NaN, undefined and null ANY VALUE THAT IS
NOT HERE IS TRUTHY VALUE
SHORTCUT OF IF STATEMENT
TERNARY OPERATOR ?:
similar to if else-statement
true ? 'truthy' : 'falsy'
1st word is the condition; ? ” if branch , : “ else branch
GUARD OPERATOR &&
DEFAULT OPERATOR ||
USE IT AS DEFAULT
FUNCTIONS
<script>
function function1(){ /original code/DOESN’T RUN
console.log('hello');
console.log(2 + 2);
}
function1(); // run the code from the orig
reus
function1();
SYNTAX RULE
THE WORD FUNCTION CREATES A FUNCTION
THE CODE INSIDE THE FUNCTION DOESN’T RUN IT CREATES
PICK VERB FOR NAMING
FUNCTIONS ALSO CREATE SCOPES
RETURN STATEMENT
<script>
function pickComputerMove(){
o CODE RUN FROM TOP TO BOTTOM
2. ALTERNATIVE ODE(RETURN)
<button onclick="
const computerMove = (pickComputerMove()); //what we return
computeMove are 2
diff
<script>
function pickComputerMove(){
const randomNumber = Math.random();
PARAMETERS
OBJECTS
price : 1040
}
console.log(product);
console.log(product.price);
Code on the left(name & price) is called PROPERTY
}
console.log(product);
console.log(product.price);
product.name = 'cottonon' ; //changed the value
console.log(product);
SYNTAX RULES
Start with a { -open then end - }
: - separate the value and a property. Al together it is called PROPERY-VALUE PAIR
Product.price – is called a DOT NOTATON or use
Bracket notation –let use properties that don’t work in DOT notation e.g
delivery-time : '11 h 0' , // doesn’t work. Think it’s a
subtract
['delivery-time'] : '11 h 0' //property value pair
}
console.log (product.delivery-time);
console.log(product['delivery-time']) //bracket notation
rating : {
stars : 4.5 ,
count : 86
}
console.log(product.rating.stars)
console.log(product.rating.count)
FUNCTIONS ARE ALSO VALUES THEY CAN BE SAVED IN A OBJECT. Function inside a object are
called(METHOD) e.g console.log is also a method(console is a object .log is a function saved
inside console object) and also Math.random
BUILD- IN OBJECTS
Math and console ARE BUILD-IN OBJECT BECAUSE THEY ARE PROVIDED BY THE LANGUAGE
JSON and localStorage are 2 MORE BUILD-I OBJECT
JSON(JavaScript Object Notation)
IT IS A SYNTAX SIMILAR TO A JAVASCRIPT OBJECT BUT LESS FEATURES E.g all properties and
strings must use “ ” . And does not support functions in the object
JAVASCRIPT OBJECT only make sense in javascript on the other hand JSON can be
understood by all programming languages
We use JSON when we send data to between computers and also when we store data
Build-in JSON object helps use convert javascript object to JSON (JSON.stringfy)
o Only support strings
JSON.stringify(product) // product value become a string
Convert JSON back to a javascript object
JSON.parse(JSON.stringify(product))
localStorage
saves values more permanently
only save strings
localStorage.setItem('message', 'hello'); //set item
3. UPDATE THE SCORE(ROCK PAPER SCISSOR)
score.win = 0;
score.lose = 0;
score.tie=0;
localStorage.removeItem('score')
">reset button</button>
win : 0,
lose : 0,
tie : 0
};
Null vs undefined
const object1 = {
message: 'hello'
};
const object2 = object1
object1.message = 'good job'
const object3 = {
message: 'goodjob'
}
console.log(object1 === object3); // false - cant compare object(ref)
directly
console/log(object1 === object2) // true –both point to the same
reference
Object1 is actually a reference the value in the object is saved in the computer memory .So
reference is like a shortcut pointing the location of the value. JUST LIKE A COMPUTER
SHORTCUT
Object2=object1 does not make a copy of the object it makes a copy of a reference point to the
object(it is called = COPY BY REFERENCE).
Both point to the same object 2 = object1 = ‘hello’
object1.message = 'good job'
o now object1 and object2 are both = ‘good job’
SHORTCUT
const object4 = {
message: 'goodjob'
}
const message = object4.message
const {message} = object4 //called destructuring
document.querySelector('.js-button') or
const buttonElement = document.querySelector('.js-button')
class attribute –start the class with a js- to make it clear it used in javascript
class selector (.js-button)-it is also used in css
DOM PROJECT
function subscribeElement(){
let buttonElement = document.querySelector('.js-subscribeButton')
SHIPPING CALCULATOR
A element can have multiple of classes you just separate with space
else{SubscribeElement.innerHTML = 'Subscribe'
SubscribeElement.classList.remove('is-subscribed')//class removed
}
i = 1 , condition i <= 5 which means 1 <= 5(true) increment step (++) increases the value
i(e.g 1,2,3,4,5,6,7…). When it reaches 6 it will be false because the condition is (i <=5)
and the loop will stop
Increment step makes sure the loop end at some point
FOR LOOP-
go through each value of an array one by one and do something with each value
const todoList = [
'make dinner', index = 0
'wash dishes', index = 1
'watch youtube' index = 2
]
ARRAY TO DO LIST
Steps(todo list)-
1. create array to to store todos
2. when we click ’add’
3. get text from textbox
4. add it to array
5. console.log() the array
function addTodo(){
const inputElement = document.querySelector('.js-name-input')
todoList.push(name);
console.log(todoList);
Steps(-
1. Loop through the array
2. Create some html code for each todo
3. Put the code on the web page
const todoList = ["dinner table", "wash dishes"];
renderTodoList();
function renderTodoList(){
let todoListHTML = '';
document.querySelector('.js-todo-list').innerHTML = todoListHTML;
}
3. 3rd step-(put the code on the web page)
todoListHTML += html
}
console.log(todoListHTML);
document.querySelector('.js-todo-list').innerHTML = todoListHTML
<div class="js-todo-list">text</div>
Date code is –
<input type="date">
Short-cut destructuring
when you get a specific value out of the array you use destructuting
const [firstvValue, secondValue] = [1 , 2 , 3]
console.log(firstvValue) //=1 ‘firstvalue’ 1st array(0)
console.log(secondValue) //=2 2nd array(1)
Or
function doubleArray(nums){
const numsDoubled = [];
ADVANCED FUNCTIONS
console.log(function1)
Anonymous function – function without a name(shortcut)
const function1 = function(){
console.log('hello')
}
console.log(function1)
function1()
o As long as the is a way to access a function the is no need to have the name of a
function
Objects and fuction
const object1 = {
num : 2,
fun : function(){ //anonymous function
console.log('hello3')
}
}
object1.fun() //excess the function
Passing a value in a function
function display(param){
console.log(param)
}
display(2);
Passing a function to another function
function run(param){
param()
}
run(function(){ //called a callback
console.log('hello4')
})
The advantage of using the old method of function is that it is already a shortcut and its easy to
read it. It also gives us a feature called hoisting
Hoisting is calling a function before you created in the code, so the order does not
matter to were you call the function
setTimeout(function(){
console.log('Timeout')
}, 3000);
Asynchronous code means wont wait for a line to finish before going to the next line
e.g(console.log(‘hello4’))
o console.log('Timeout') will display after the line(3 second delay),
console.log('hello4')) this line is going to display 1st
Synchronous code mean it will wait for the line to finish before going to the next line
o All the code we written in this course are synchronous code
Code is only asynchronous when we use setTimeout
o Advantage of asynchronous code is that it doesn’t block other code for 3 second
setInterval(function(){
console.log('Interval')
}, 3000)
every 3 seconds the code will run
function autoPlay(){
setInterval(function(){
const playGame = computerRandom();
gameMove(playGame);
}, 1000)
}
keep track whether we are playing or not
let isAutoPlaying = false;
let intervalId
function autoPlay(){
if(!isAutoPlaying){
intervalId = setInterval(function(){
const playGame = computerRandom();
gameMove(playGame)
}, 1000);
isAutoPlaying = true;
}else{
clearInterval(intervalId)
isAutoPlaying = false
}
}
setInterval () returns a number and this number is like an iD and this iD can used to stop
the interval (clearInterval())
previously when you loop trough an array we used a for loop and a while loop
another way is using .ForEach()
forEach is preferred than for loop
[
'make dinner',
'wash dishes',
'watch youtube'
].forEach(function(value, index){ //value-parameter
for[]
console.log(index) //index-position of each
array
console.log(value)
})
forEach needs a parameter(value) to loop through all the array and foreach of the
values it will save the values in the parameters the runs it
the 2nd value in forEach() is index
function renderTodoList(){
todoList.forEach(function(todoObject, index){
const html = `
<p>${name} ${dueDate}
<button onclick="
todoList.splice(${index}, 1)
renderTodoList();
">Delete</button>
</p>
`;
todoListHTML += html;
})
document.querySelector('.js-todo-list').innerHTML = todoListHTML
}
ARROW FUNCTIONS
console.log('hello')
return 5;
}
difference is that arrow functions have some shortcut that doesn’t work on a regular function
SHORTCUTS
.addEventListener()
buttonElement.addEventListener('click', () => {
console.log('click2')
})
Advantages of .addEventListener()
Todolist(addEventListener)
document.querySelectorAll('js-delete-todo-button')
.forEach((deleteButton, index) => {
deleteButton.addEventListener('click', () => {
todoList.splice(index, 1);
renderTodoList();
})
})
.querySelectorAll() – gives all the categories that match js-‘’ ,event if it’s a string