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.
96 lines
3.0 KiB
96 lines
3.0 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">Code Sample 1</h2> |
|
<p>This is the JavaScript code for 08_10 in the exercise files. </p> |
|
<h2 class="sectiontitle">Callback Function</h2> |
|
<pre class="codesample"> |
|
/** |
|
* Using a callback function. |
|
* @link https://developer.mozilla.org/en-US/docs/Glossary/Callback_function |
|
*/ |
|
|
|
// Helper function to format currency numbers. Used by tipCalculator |
|
const formatter = (locale = "en-US", currency = "USD", value) => { |
|
let formattedValue = new Intl.NumberFormat(locale, { |
|
style: "currency", |
|
currency: currency, |
|
}).format(value); |
|
|
|
return formattedValue; |
|
}; |
|
|
|
// Callback receives finalTip object, creates and outputs table on the DOM. |
|
const printHTML = (finalTip) => { |
|
const tipTable = document.createElement("table"); |
|
tipTable.innerHTML = ` |
|
<tr> |
|
<td>Sum before tip:</td> |
|
<td>${finalTip.sum}</td> |
|
</tr> |
|
<tr> |
|
<td>Tip percentage:</td> |
|
<td>${finalTip.percentage}</td> |
|
</tr> |
|
<tr> |
|
<td>Tip:</td> |
|
<td>${finalTip.tip}</td> |
|
</tr> |
|
<tr> |
|
<td>Total:</td> |
|
<td>${finalTip.total}</td> |
|
</tr> |
|
`; |
|
document.querySelector("main").append(tipTable); |
|
}; |
|
|
|
// Create a finalTip object with all the data. Send it to the printHTML callback. |
|
const tipCalculator = (sum, percentage, locale, currency) => { |
|
let tip = sum * (percentage / 100); |
|
let total = sum + tip; |
|
|
|
const finalTip = { |
|
sum: formatter(locale, currency, sum), |
|
percentage: percentage + "%", |
|
tip: formatter(locale, currency, tip), |
|
total: formatter(locale, currency, total), |
|
}; |
|
}; |
|
|
|
tipCalculator(29.95, 18, "de-DE", "EUR");</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> |