/* scripts.js JavaScript examples Sparisoma Viridi | dudung@gmail.com 20180116 Create some examples. */ // Execute a test function test_temperature_conversion(); // 20180120.1919 !ok function test_temperature_conversion() { // Define some temperature references in oC and oF var THC = 100; var TCC = 0; var THF = 212; var TCF = 32; // Convert oC to oF and then back to oC var TC = 20; var TF = C2F(TC); console.log("T = " + TF + " oF"); var TC2 = F2C(TF); console.log("T = " + TC2 + " oC"); // Convert from oC to oF function C2F(TC) { var TF = (TC - TCC) / (THC - TCC) * (THF - TCF) + TCF; return TF; } // Convert from oF to oC function F2C(TF) { var TC = (TF - TCF) / (THF - TCF) * (THC - TCC) + TCC; return TC; } } // 20180116.0950 ok function test_table() { var table = document.createElement("table"); table.border = "1"; document.body.appendChild(table); var row1 = document.createElement("tr"); table.appendChild(row1); var col11 = document.createElement("td"); row1.appendChild(col11); col11.innerHTML = "(1,1) A"; var col12 = document.createElement("td"); row1.appendChild(col12); col12.innerHTML = "(1,2) B"; var row2 = document.createElement("tr"); table.appendChild(row2); var col21 = document.createElement("td"); row2.appendChild(col21); col21.innerHTML = "(2,1) C"; var col22 = document.createElement("td"); row2.appendChild(col22); col22.innerHTML = "(2,2) D"; } // 20180116.0941 ok function test_canvas_circles() { var c = document.createElement("canvas"); c.width = 300; c.height = 200; c.style.background = "#eee"; var cx = c.getContext("2d"); for(var x = 50; x < 300; x += 50) { for(var y = 50; y < 200; y += 50) { var R = parseInt(Math.random() * 256); var G = parseInt(Math.random() * 256); var B = parseInt(Math.random() * 256); var colorRGB = "rgb(" + R + "," + G + "," + B + ")"; cx.strokeStyle = colorRGB; cx.beginPath(); cx.arc(x, y, 20, 0, 2 * Math.PI); cx.stroke() } } document.body.appendChild(c); } // 20180116.0936 ok function test_canvas() { var c = document.createElement("canvas"); c.width = 300; c.height = 200; c.style.background = "#eee"; var cx = c.getContext("2d"); cx.strokeStyle = "#f00"; cx.beginPath(); cx.arc(150, 100, 50, 0, 2 * Math.PI); cx.stroke() document.body.appendChild(c); } // 20180116.0927 !ok function test_bilangan_acak() { var N = 10; for(var i = 0; i < N; i++) { var x = Math.random(); var y = parseInt(x * 10); console.log(i + "\t" + x + "\t" + y); } } // 20180116.0918 function test_create_button_from_parent() { // Define first id var id = "10"; // Create first button var btn = document.createElement("button"); btn.id = 10 + parseInt(Math.random() * 90); btn.innerHTML = btn.id; document.body.appendChild(btn); // Add event btn.addEventListener("click", createButton); // Create a button function createButton() { var btn = document.createElement("button"); var id = 10 + parseInt(Math.random() * 90); var parentId = event.target.id; while(id == parentId) { id = 10 + parseInt(Math.random() * 90); } btn.id = id; btn.innerHTML = btn.id + " [" + parentId + "]"; document.body.appendChild(btn); btn.addEventListener("click", createButton); } } // 20180116.0850 ok function test_button_create_button() { // Define button number var buttonNumber = 1; // Create first visible button var btn = document.createElement("button"); btn.innerHTML = "Create another"; document.body.appendChild(btn); // Add an event only to to first button btn.addEventListener("click", createAnotherButton); // Create another button function createAnotherButton() { var btn = document.createElement("button"); btn.innerHTML = "Another button " + buttonNumber; document.body.appendChild(btn); buttonNumber++; } } // 20180116.0842 ok function test_hello_button() { // Create element of type button, which is // in a HTML page var b = document.createElement("button"); b.innerHTML = "Hello, World!"; document.body.appendChild(b); } // 20180116.0840 function test_hello_paragraph() { // Create element of type paragraph, which is

// in a HTML page var p = document.createElement("p"); p.innerHTML = "Hello, World!"; document.body.appendChild(p); } // 20180116.0824 ok function test_hello_title() { // Tags inside section should // already be defined var title = document.getElementsByTagName("title"); title[0].text = "Hello, Wordl!"; } // 20180116.0818 ok function test_hello_console() { // Open concole in internet browser // Google Chrome: Ctrl+Shift+I, Ctrl+Shift+J // Mozilla Firefox: Ctrl+Shift+I // Internet Explorer: F12 then Ctrl+2 console.log("Hello, World!"); }