You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.6 KiB
130 lines
3.6 KiB
<!doctype html> |
|
<html> |
|
<head> |
|
<meta charset="utf-8"> |
|
<meta http-equiv="X-UA-Compatible" content="IE=edge"> |
|
<meta name="viewport" content="width=device-width, initial-scale=1"> |
|
<title>My Learning Website</title> |
|
<link href="/styles/styles.css" rel="stylesheet" type="text/css"> |
|
<link href="/webdevelopment/styles/styles.css" rel="stylesheet" type="text/css"> |
|
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> |
|
<!-- WARNING: Respond.js doesn't work if you view the page via file:// --> |
|
<!--[if lt IE 9]> |
|
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> |
|
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> |
|
<![endif]--> |
|
</head> |
|
<body> |
|
|
|
<h1 class="courselink">JavaScript Essential Training</h1> |
|
<h2 class="lecturer">LinkedIn Learning : Morten Rand-Hendriksen</h2> |
|
<h1 class="episodetitle">Code Samples</h1> |
|
|
|
<article> |
|
<h2 class="sectiontitle">Looping Through Content</h2> |
|
<p>This is the JavaScript code for 08_14 in the exercise files. </p> |
|
<h2 class="sectiontitle">Variations of the for loop</h2> |
|
<pre class="codesample"> |
|
/** |
|
* Loops Aplenty! |
|
* @link https://developer.mozilla.org/en-US/docs/Glossary/Callback_function |
|
*/ |
|
|
|
const stuff = ["piggy", "headlamp", "pen", "pencil", "eraser", "water bottle"]; |
|
|
|
const nestedObjects = { |
|
item01: { |
|
name: "piggy", |
|
type: "toy", |
|
weight: 30, |
|
}, |
|
item02: { |
|
name: "headlamp", |
|
type: "equipment", |
|
weight: 120, |
|
}, |
|
item03: { |
|
name: "pen", |
|
type: "tool", |
|
weight: 30, |
|
}, |
|
item04: { |
|
name: "pencil", |
|
type: "tool", |
|
weight: 30, |
|
}, |
|
item05: { |
|
name: "eraser", |
|
type: "tool", |
|
weight: 40, |
|
}, |
|
item03: { |
|
name: "water bottle", |
|
type: "equipment", |
|
weight: 1300, |
|
}, |
|
}; |
|
|
|
const article = document.querySelector("article"); |
|
let stuffList = document.createElement("ul"); |
|
|
|
|
|
/** |
|
* for loop |
|
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for |
|
*/ |
|
for (let i = 0; i < stuff.length; i++) { |
|
let listItem = document.createElement("li"); |
|
listItem.innerHTML = stuff[i]; |
|
|
|
stuffList.append(listItem); |
|
} |
|
|
|
/** |
|
* for...of loop and arrays |
|
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of |
|
*/ |
|
for (const item of stuff) { |
|
let listItem = document.createElement("li"); |
|
listItem.innerHTML = item; |
|
stuffList.append(listItem); |
|
} |
|
|
|
/** |
|
* foreach array method |
|
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach |
|
*/ |
|
stuff.forEach((item) => { |
|
let listItem = document.createElement("li"); |
|
listItem.innerHTML = item; |
|
stuffList.append(listItem); |
|
}); |
|
|
|
/** |
|
* for...in loop and objects |
|
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of |
|
*/ |
|
for (const singleObject in nestedObjects) { |
|
let listItem = document.createElement("li"); |
|
listItem.innerHTML = `Name: ${nestedObjects[singleObject].name} : Type: ${nestedObjects[singleObject].type} : Weight: ${nestedObjects[singleObject].weight}`; |
|
stuffList.append(listItem); |
|
} |
|
|
|
|
|
article.append(stuffList);</pre> |
|
</article> |
|
|
|
<div class="btngroup"> |
|
<button class="button" onclick="window.location.href='javascriptessentialtraining2021.html'"> |
|
Course Contents |
|
</button> |
|
<button class="button" onclick="window.location.href='/webdevelopment/webdevelopment.html'"> |
|
Web Development Page |
|
</button> |
|
<button class="button" onclick="window.location.href='/index.html'"> |
|
Home |
|
</button> |
|
</div> |
|
|
|
</body> |
|
</html> |