Commit 2dc14844 authored by Alex Segers's avatar Alex Segers

Initial commit [@asegers]

parents
<!doctype html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>JavaScript Basics | Alex Segers</title>
<!-- Variables Exercise (1/2) -->
<script type="text/javascript">
var global = " GLOBAL "
function inMyHead() {
var local = " local "
document.write(global);
document.write(local);
}
</script>
</head>
<body>
<h1>JavaScript Basics | <a href="https://gitlab.mynisum.com/asegers/javascript-basics" target="_blank">Alex Segers</a></h1>
<!-- Variables Exercise (2/2) -->
<script type="text/javascript">
inMyHead();
document.write(global);
try {
document.write(local);
} catch (err) {
var msg = `${err.name}: ${err.message}`
console.error(msg, msg === err.toString())
}
</script>
<!-- IIFE & Anon Func Exercise -->
<script type="text/javascript">
document.write(function (name, session){
return `Hello ${name}! Welcome to ${session}.`;
}("Alex", 'JavaScript Basics'));
var myAnonFunc = function () {}
</script>
<!-- Loops & switch Statement -->
<script type="text/javascript">
const monthsArr = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
]
// switch Statement
function printQuarter(monthName) {
switch (monthName) {
case "January": case "February": case "March":
console.log("Q1");
break;
case "April": case "May": case "June":
console.log("Q2");
break;
case "July": case "August": case "September":
console.log("Q3");
break;
case "October": case "November": case "December":
console.log("Q4");
break;
default:
if (/^([1-9]|1[012])$/.test(monthName)) {
printQuarter(monthsArr[monthName])
} else {
console.log("Not a valid month");
}
}
}
printQuarter(monthsArr[2]); // -> Q1
printQuarter(9); // -> Q4
// while Loop
let i = 0;
while (i < monthsArr.length) {
console.log(i, monthsArr[i]); // -> 0 January, 1 February, etc ..
i++;
}
// do-while Loop
let idx;
do {
if (idx === undefined) idx = 0;
console.log(idx, monthsArr[idx]); // -> 0 January, 1 February, etc ..
idx++;
} while (idx < monthsArr.length);
// for Loop
for (let i = 0; i < monthsArr.length; i++)
console.log(i, monthsArr[i]); // -> 0 January, 1 February, etc ..
// for-in Loop
for (const idxStr in monthsArr) {
console.log(idxStr, typeof idxStr === "string"); // -> 0 true, 1 true, etc ...
}
// for-of Loop
for (const month of monthsArr)
console.log(month); // -> January, February, etc ...
// for-in (w/ object)
const monthsObj = Object.assign({}, monthsArr);
for (const key in monthsObj)
console.log(`${key}: ${monthsObj[key]}`); // -> 0: January, 1: February, etc ..
</script>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment