Math.random( ) --> [0, 1)
Math.random( )*256 --> [0, 255)
Math.floor(Math.random( )*256) --> {0, 1,...,254,255},
which are the random outputs that we want for
a random color component.
Now convert the base-10 random values to hex and
concatenate them to obtain the hex color code.
arr.splice(start, len, item1, item2, ... , itemn);where start is the index where the section of the array to remove begins
this.balls.splice(randVal, 1);this.balls is the array of Lotto balls still available to draw. When the ball with index randVal is drawn it must be removed from this.balls. There is 1 ball to remove, and no balls to insert so there are no extra items in the splice call. Here is a sequence that shows how the balls and ballsDrawn arrays are used to draw five balls out of 10 possible balls. The variable i is the index of the for loop that draws the balls:
i ballsDrawn balls
---+------------+-------------------
[ ] [1,2,3,4,5,6,7,8,9,10]
1 [4] [1,2,3,5,6,7,8,9,10]
2 [4,9] [1,2,3,5,6,7,8,10]
3 [4,9,5] [1,2,3,6,7,8,10]
4 [4,9,5,7] [1,2,3,6,8,10]
5 [4,9,5,7,1] [2,3,6,8,10]
<img id="1">before any image file is displayed in it. An image file is defined as
var imgFile = "images/5.jpg";To display the image file in the image element, you can do this in JavaScript:
var imgElement = document.getElementById("1");
imgElement.src = imgFile;
The script for the Lotto class displays a random image file in the image element
chosen randomly without replacement.
class Pair {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString( ) {
return `(${this.x}, ${this.y})`;
}
}
A Pair object is an ordered pair of values that you used in high school math class.
var p1 = new Pair(3, 5);
var p2 = new Pair(4, 7);
var p3 = new Pair(6, 11);
document.writeln(`${p1}, ${p2}, ${p3}`);
// Output: (3, 5), (4, 7), (6, 11)
<input> <button> <textarea> <select> <option>HTML Tags Listed Alphabetically (W3Schools)
id class hidden style disabled dragableHTML Global Attributes (W3Schools)
<input> type (button checkbox radio hidden number password
color date email file image month range reset
search submit tel text time url week)
value (text)
checked (checkbox)
max (number)
maxlength (number)
minlength (number)
multiple (text)
pattern (text)
placeholder (text)
readonly (text)
size (number)
step (number)
<button>
<textarea> cols
rows
readonly
maxlength
placeholder
wrap
<select> multiple
size
<option> value
selected
Tag Attibutes Listed Alphabetically (W3Schools)blur change click dblclick focus keydown keypress keyup load mousedown mouseenter mouseleave mousemove mouseup resize scroll select wheel
HTML Events (W3Schools)
--------------------------------
<!DOCTYPE html>
<html>
<head>
<title>Test Focus/Blur</head>
<script src="script.js"></script>
<body>
<h1>Test Focus/Blur</h1>
<input type="text" id="tf1"><br><br>
<p id="p1"></p>
</body>
</html>
--------------------------------
function focusListener( ) {
var para = document.getElementById("p1");
para.innerHTML = "Focus event fired.";
}
function blurListener( ) {
var para = document.getElementById("p1");
para.innerHTML = "Blur event fired.";
}
function init( ) {
var textField = document.getElementById("tf1");
textField.addEventListener("focus", focusListener);
textField.addEventListener("focus", blurListener);
}
window.addEventListener("load", init);
--------------------------------
h1 p td li span
<body>
<div id="content">
<h1>Sample HTML Page</h1>
<p class="info">Lorum ipsum dolor sit amet.</p>
<p>Lorum ipsum dolor <a href="#">sit</a> amet.</p>
</div>
<div id="nav">
<ul>
<li><a href="#" class="intro">Item 1<a></li>
<li><a href="#">Item 2</li>
<li><a href="#">Item 3</li>
</ul>
</div>
<div id="footer">
Lorum ipsum dolor <a href="#">sit</a> amet.
</div>
</body>
Here are some CSS selectors:
li { color: blue; }
.intro { font-weight: bold; }
p.intro { color: red; }
a.intro { color: green; }
#nav { color: blue; }
li a { color: green; }
#nav a { color: red; }
#nav ul li a { color: green; }
* { margin: 0px; padding: 0px; }
div > em { color: blue; }
h2 + h3 { margin: -1em; }
img[src="small.gif"] { border: 1px solid #000000; }
p:first-line { font-weight: bold; }
p:first-letter { font-size: 200%; font-weight: bold; }
<body>
<h1>Planetary Outpost Status Summaries</h1>
<ol>
<li>Green Planet<br>
All is well.</li>
<li class="redtext">Red Planet<br>
All systems A-OK.</p>
</ol>
<h2>Number of Planets</h2>
<p id="para">3</p>
</body>
+--------- body ---------+
| / \ |
| / \ |
h1 ol h2 p
/ \
li li
body
h1
Planetary Outpost Status Summaries
ol
li
Green Planet<br>All is well.
li
Red Planet<br>All systems A-OK.
h2
Number of Planets
p
3
document.getElementById("123")
document.getElementsByTagName("input")
document.getElementsByClassName("redtext")
document.querySelector("li .redtext");
document.querySelectorAll("li .redtext");
innerHTML nodeName firstChild lastChild childNodes previousSibling nextSibling
element.attributeName = "HTMLAttributeValue"; element.style.styleName = "cssStyleValue";
document.createElement(element); document.removeChild(element); document.appendChild(element); document.replaceChild(oldElement, newElement);
<p id="output" />
var elements = document.getElementsByTagName("html");
var para = document.getElementById("output");
para.innerHTML = elements[0].nodeName;
var elements = document.getElementsByTagName("li");
var para = document.getElementById("output");
para.innerHTML = elements.length;
<body />
This is a test.Answer: The answer to this problem is in the answer for Part b.
| Name | Height | Country |
|---|---|---|
| Kilimanjaro | 5895 | Tanzania |
<!DOCTYPE html>
<html>
<head>
<script>
function init( ) {
// Create paragraph and add to body.
var b = document.getElementsByTagName("body")[0];
var p = document.createElement("p");
p.innerHTML = "This is a test.";
b.appendChild(p);
// Create table
var t = document.createElement("table");
var r1 = document.createElement("tr");
var r2 = document.createElement("tr");
t.appendChild(r1);
t.appendChild(r2);
b.appendChild(t);
// Add data to row 1.
var th1 = document.createElement("th");
var th2 = document.createElement("th");
var th3 = document.createElement("th");
th1.innerHTML = "Name";
th2.innerHTML = "Height";
th3.innerHTML = "Country";
r1.appendChild(th1);
r1.appendChild(th2);
r1.appendChild(th3);
// Add data to row 2 using a data array.
var data = ["Kilamanjaro", "5895", "Tanzania"];
for(let i = 0; i <= 2; i++) {
var td = document.createElement("td");
r2.appendChild(td);
td.innerHTML = data[i];
td.style.borderStyle = "solid";
}
t.appendChild(r2);
t.style.borderCollapse = "collapse";
}
window.addEventListener("load", init);
</script>
</head>
<body></body>
</html>
document.write(Boolean(5) + " ");
document.write(Boolean(283764) + " ");
document.write(Boolean(0) + " ");
document.write(Boolean(-203) + "<br>");
// Output: true true false true
document.write(Boolean(3.14159265) + " ");
document.write(Boolean(-284.378) + " ");
document.write(Boolean(0.000000001) + " ");
document.write(Boolean(0.0) + "<br>");
// Output: true true true false
document.write(Boolean("grapefruit") + " ");
document.write(Boolean("$%&*@") + " ");
document.write(Boolean("0") + " ");
document.write(Boolean("false") + " ");
document.write(Boolean(" ") + " ");
document.write(Boolean("") + "<br>");
// Output: true true true true false
document.write(Boolean(null) + " ");
document.write(Boolean(undefined) + " ");
document.write(Boolean(Infinity) + " ");
document.write(Boolean(NaN) + "<br>");
// Output: false false true false
Then run it to check if your prediction is correct.
var n = 45;
var v;
if (n) {
v = "nonzero";
}
else {
v = "zero";
}
document.writeln("Value: " + v);