Merge branch 'main' of https://github.com/PacktPublishing/JavaScript-from-Beginner-to-Professional into main
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Tester</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<script>
|
||||
console.log("hello world");
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Tester</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<script src="app.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,7 @@
|
||||
let a = 10; // assign a value of 10 to variable a
|
||||
console.log(a); // This will output 10 into the console
|
||||
/*
|
||||
This is a multi-line
|
||||
Comment
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>JS Tester</title>
|
||||
</head>
|
||||
<body>
|
||||
<script src="myJS.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
// console.log("Laurence");
|
||||
/*
|
||||
This is my comment
|
||||
Laurence Svekis
|
||||
*/
|
||||
@@ -1 +1,13 @@
|
||||
let str1 = 'Laurence';
|
||||
let str2 = "Svekis";
|
||||
let val1 = undefined;
|
||||
let val2 = null;
|
||||
let myNum = 1000;
|
||||
|
||||
|
||||
console.log(typeof(str1));
|
||||
console.log(typeof(str2));
|
||||
console.log(typeof(val1));
|
||||
console.log(typeof(val2));
|
||||
console.log(typeof(myNum));
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
const myName = "Maaike";
|
||||
const myAge = 29;
|
||||
const coder = true;
|
||||
const message = "Hello, my name is " + myName + ", I am " + myAge+" years old and I can code JavaScript: " + coder + ".";
|
||||
console.log(message);
|
||||
@@ -0,0 +1,6 @@
|
||||
let a = window.prompt("Value 1?");
|
||||
let b = window.prompt("Value 2?");
|
||||
a = Number(a);
|
||||
b = Number(b);
|
||||
let hypotenuseVal = ((a * a) + (b * b))**0.5;
|
||||
console.log(hypotenuseVal);
|
||||
@@ -0,0 +1,7 @@
|
||||
let a = 4;
|
||||
let b = 11;
|
||||
let c = 21;
|
||||
a = a + b;
|
||||
a = a / c;
|
||||
c = c % b;
|
||||
console.log(a, b, c);
|
||||
@@ -0,0 +1,5 @@
|
||||
//Convert miles to kilometers.
|
||||
//1 mile equals 1.60934 kilometers.
|
||||
let myDistanceMiles = 130;
|
||||
let myDistanceKM = myDistanceMiles * 1.60934;
|
||||
console.log("The distance of " + myDistanceMiles + " miles is equal to " + myDistanceKM + " kilometers")
|
||||
@@ -0,0 +1,9 @@
|
||||
//1 inch = 2.54 centimetres.
|
||||
//2.2046 pounds in a kilo
|
||||
let inches = 72;
|
||||
let pounds = 180;
|
||||
let weight = pounds / 2.2046; // in kilos
|
||||
let height = inches * 2.54; // height in centimetres
|
||||
console.log(weight, height);
|
||||
let bmi = weight/(height/100*height/100);
|
||||
console.log(bmi);
|
||||
@@ -0,0 +1,4 @@
|
||||
const myList = ["Milk", "Bread", "Apples"];
|
||||
console.log(myList.length);
|
||||
myList[1] = "Bananas";
|
||||
console.log(myList);
|
||||
@@ -0,0 +1,12 @@
|
||||
const myList = [];
|
||||
myList.push("Milk", "Bread", "Apples");
|
||||
myList.splice(1, 1, "Bananas", "Eggs");
|
||||
const removeLast = myList.pop();
|
||||
console.log(removeLast);
|
||||
myList.sort();
|
||||
console.log(myList.indexOf("Milk"));
|
||||
myList.splice(1, 0, "Carrots", "Lettuce");
|
||||
const myList2 = ["Juice", "Pop"];
|
||||
const finalList = myList.concat(myList2, myList2);
|
||||
console.log(finalList.lastIndexOf("Pop"));
|
||||
console.log(finalList);
|
||||
@@ -0,0 +1,5 @@
|
||||
const myArr = [1, 2, 3];
|
||||
const bigArr = [myArr, myArr, myArr];
|
||||
console.log(bigArr[1][1]);
|
||||
console.log(bigArr[0][1]);
|
||||
console.log(bigArr[2][1]);
|
||||
@@ -0,0 +1,15 @@
|
||||
const myCar = {
|
||||
make: "Toyota",
|
||||
model: "Camry",
|
||||
tires: 4,
|
||||
doors: 4,
|
||||
color: "blue",
|
||||
forSale: false
|
||||
};
|
||||
|
||||
let propColor = "color";
|
||||
myCar[propColor] = "red";
|
||||
propColor = "forSale";
|
||||
myCar[propColor] = true;
|
||||
console.log(myCar.make + " " + myCar.model);
|
||||
console.log(myCar.forSale);
|
||||
@@ -0,0 +1,6 @@
|
||||
const people = {friends:[]};
|
||||
const friend1 = {first: "Laurence", last: "Svekis", id: 1};
|
||||
const friend2 = {first: "Jane", last: "Doe", id: 2};
|
||||
const friend3 = {first: "John", last: "Doe", id: 3};
|
||||
people.friends.push(friend1, friend2, friend3);
|
||||
console.log(people);
|
||||
@@ -0,0 +1,7 @@
|
||||
theList.pop();
|
||||
theList.shift();
|
||||
theList.unshift("FIRST");
|
||||
theList[3] = "hello World";
|
||||
theList[2] = "MIDDLE";
|
||||
theList.push("LAST");
|
||||
console.log(theList);
|
||||
@@ -0,0 +1,22 @@
|
||||
const inventory = [];
|
||||
const item3 = {
|
||||
name: "computer",
|
||||
model: "imac",
|
||||
cost: 1000,
|
||||
qty: 3
|
||||
}
|
||||
const item2 = {
|
||||
name: "phone",
|
||||
model: "android",
|
||||
cost: 500,
|
||||
qty: 11
|
||||
}
|
||||
const item1 = {
|
||||
name: "tablet",
|
||||
model: "ipad",
|
||||
cost: 650,
|
||||
qty: 1
|
||||
}
|
||||
inventory.push(item1, item2, item3);
|
||||
console.log(inventory);
|
||||
console.log(inventory[2].qty);
|
||||
@@ -0,0 +1,8 @@
|
||||
const test = false;
|
||||
console.log(test);
|
||||
if(test){
|
||||
console.log("It's True");
|
||||
}
|
||||
if(!test){
|
||||
console.log("False now");
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
let age = prompt("How old are you?");
|
||||
age = Number(age);
|
||||
let message;
|
||||
if(age >= 21){
|
||||
message = "You can enter and drink.";
|
||||
}else if(age >= 19){
|
||||
message = "You can enter but not drink.";
|
||||
}else{
|
||||
message = "You are not allowed in!";
|
||||
}
|
||||
console.log(message);
|
||||
@@ -0,0 +1,3 @@
|
||||
const id = true;
|
||||
const message = (id) ? "Allowed In" : "Denied Entry";
|
||||
console.log(message);
|
||||
@@ -0,0 +1,21 @@
|
||||
const randomNumber = Math.floor(Math.random() * 6);
|
||||
let answer = "Something went wrong";
|
||||
let question = prompt("Ask me anything");
|
||||
switch (randomNumber) {
|
||||
case 0:
|
||||
answer = "It will work out";
|
||||
break;
|
||||
case 1:
|
||||
answer = "Maybe, maybe not";
|
||||
break;
|
||||
case 2:
|
||||
answer = "Probably not";
|
||||
break;
|
||||
case 3:
|
||||
answer = "Highly likely";
|
||||
break;
|
||||
default:
|
||||
answer = "I don't know about that";
|
||||
}
|
||||
let output = "You asked me " + question + ". I think that " + answer;
|
||||
console.log(output);
|
||||
@@ -0,0 +1,22 @@
|
||||
let prize = prompt("Pick a number 0-10");
|
||||
prize = Number(prize);
|
||||
let output = "My Selection: ";
|
||||
switch (prize){
|
||||
case 0:
|
||||
output += "Gold ";
|
||||
case 1:
|
||||
output += "Coin ";
|
||||
break;
|
||||
case 2:
|
||||
output += "Big ";
|
||||
case 3:
|
||||
output += "Box of ";
|
||||
case 4:
|
||||
output += "Silver ";
|
||||
case 5:
|
||||
output += "Bricks ";
|
||||
break;
|
||||
default:
|
||||
output += "Sorry Try Again";
|
||||
}
|
||||
console.log(output);
|
||||
@@ -0,0 +1,13 @@
|
||||
let val = prompt("What number?");
|
||||
val = Number(val);
|
||||
let num = 100;
|
||||
let message = "nothing";
|
||||
if (val > num) {
|
||||
message = val + " was greater than " + num;
|
||||
} else if (val === num) {
|
||||
message = val + " was equal to " + num;
|
||||
} else {
|
||||
message = val + " is less than " + num;
|
||||
}
|
||||
console.log(message);
|
||||
console.log(message);
|
||||
@@ -0,0 +1,13 @@
|
||||
let person = prompt("Enter a name");
|
||||
let message;
|
||||
switch (person) {
|
||||
case "John" :
|
||||
case "Larry" :
|
||||
case "Jane" :
|
||||
case "Laurence" :
|
||||
message = person + " is my friend";
|
||||
break;
|
||||
default :
|
||||
message = "I don't know " + person;
|
||||
}
|
||||
console.log(message);
|
||||
@@ -0,0 +1,20 @@
|
||||
const myArr = ["Rock", "Paper", "Scissors"];
|
||||
let computer = Math.floor(Math.random() * 3);
|
||||
let player = Math.floor(Math.random() * 3);
|
||||
let message = "player " + myArr[player] + " vs computer " + myArr[computer] + " ";
|
||||
if (player === computer) {
|
||||
message += "it's a tie";
|
||||
} else if (player > computer) {
|
||||
if (computer == 0 && player == 2) {
|
||||
message += "Computer Wins";
|
||||
} else {
|
||||
message += "Player Wins";
|
||||
}
|
||||
} else {
|
||||
if (computer == 2 && player == 0) {
|
||||
message += "Player Wins";
|
||||
} else {
|
||||
message += "Computer Wins";
|
||||
}
|
||||
}
|
||||
console.log(message);
|
||||
Reference in New Issue
Block a user