var a = [45, 97, 12, 30, 51, 3, 61];Write a for loops that computes these values:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exercise 1</title>
<style>
ol { list-style-type: lower-alpha; }
</style>
<script>
function performComputations( ) {
var a = [45, 97, 12, 30, 51, 3, 61];
var bodyObj = document.querySelector("body");
var olObj = document.createElement("ol");
bodyObj.appendChild(olObj);
// Question 1a
var count1 = 0;
for(var n of a) {
count1++;
}
// Can also obtain count1 with this single statement:
// var count1 = a.length;
var liObj = document.createElement("li");
liObj.innerHTML = count1;
olObj.appendChild(liObj);
// Question 1b
var count2 = 0;
for(var n of a) {
if (40 <= n && n <= 60) {
count2++;
}
}
var liObj = document.createElement("li");
liObj.innerHTML = count2;
olObj.appendChild(liObj);
// Question 1c
var count = 0;
var sum = 0.0;
for(var n of a) {
count++;
sum += n;
}
var average = sum / count;
var liObj = document.createElement("li");
liObj.innerHTML = average.toFixed(2);
olObj.appendChild(liObj);
}
window.addEventListener("load", performComputations);
</script>
</head>
<body>
<h1>Exercise 1</h1>
</body>
</html>
$( function( ) {
$("img").mouseenter( function( ) {
$(this).css("width", "200px");
});
$("img").mouseleave( function( ) {
$(this).css("width", "150px");
});
};
Translate this script to vanilla JavaScript. Here is the entire HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exercise 3</title>
<style>
img { width: 150px; }
</style>
<script src="jquery-3.7.1min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function( ) {
var imgArray = document.querySelectorAll("img");
for(var anImg of imgArray) {
anImg.addEventListener("mouseenter", function(e) {
e.target.style.width = "200px";
});
}
for(var anImg of imgArray) {
anImg.addEventListener("mouseleave", function(e) {
e.target.style.width = "150px";
});
}
});
</script>
</head>
<body>
<h1>Exercise 3</h1>
<img src="images/elephant.jpg" alt="Elephant">
<img src="images/lion.jpg" alt="Lion">
<img src="images/monkey.jpg" alt="Monkey">
<img src="images/rhinoceros.jpg" alt="Rhinoceros">
</body>
</html>
const correctAnswers = ["b", "c", "a"];Start with this HTML code for an ordered list:
<ol>
<li>First Question.<br>
<ol>
<li><input type="radio" id="11" name="q1" value="a">
Answer A.</li>
<li><input type="radio" id="12" name="q1" value="b">
Answer B.</li>
<li><input type="radio" id="13" name="q1" value="c">
Answer C.</li>
</ol>
</li>
<li>Second Question.<br>
<ol>
<li><input type="radio" id="21" name="q2" value="a">
Answer A.</li>
<li><input type="radio" id="22" name="q2" value="b">
Answer B.</li>
<li><input type="radio" id="23" name="q2" value="c">
Answer C.</li>
</ol>
</li>
<li>Third Question.<br>
<ol>
<li><input type="radio" id="31" name="q3" value="a">
Answer A.</li>
<li><input type="radio" id="32" name="q3" value="b">
Answer B.</li>
<li><input type="radio" id="33" name="q3" value="c">
Answer C.</li>
</ol>
</li>
</ol>
<button id="btn1">Compute Score</button>
<p id="numcorrect"></p>
Answer: Here are the complete HTML, CSS, and JavaScript files:
==================================================
<!DOCTYPE html>
<!-- Source code file: index.html -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
</head>
<body>
<ol>
<li>First Question.<br>
<ol>
<li><input type="radio" id="11" name="q1" value="a">
Answer A.</li>
<li><input type="radio" id="12" name="q1" value="b">
Answer B.</li>
<li><input type="radio" id="13" name="q1" value="c">
Answer C.</li>
</ol>
</li>
<li>Second Question.<br>
<ol>
<li><input type="radio" id="21" name="q2" value="a">
Answer A.</li>
<li><input type="radio" id="22" name="q2" value="b">
Answer B.</li>
<li><input type="radio" id="23" name="q2" value="c">
Answer C.</li>
</ol>
</li>
<li>Third Question.<br>
<ol>
<li><input type="radio" id="31" name="q3" value="a">
Answer A.</li>
<li><input type="radio" id="32" name="q3" value="b">
Answer B.</li>
<li><input type="radio" id="33" name="q3" value="c">
Answer C.</li>
</ol>
</li>
</ol>
<button id="btn1">Compute Score</button>
<p id="numcorrect"></p>
</body>
</html>
--------------------------------------------------
/* Source code file: styles.css */
ol ol { list-style-type: lower-alpha; }
--------------------------------------------------
// Source code file: script.js
function computeScore( ) {
const correctAnswers = ["", "b", "c", "a"];
var numCorrect = 0;
for(var i = 1; i <= 3; i++) {
for(var j = 1; j <= 3; j++) {
var rb = document.getElementById("" + i + j);
if (rb.checked && rb.value == correctAnswers[i]) {
numCorrect++;
}
}
}
var outputPara = document.getElementById("numcorrect");
outputPara.innerHTML = "Number correct: " + numCorrect;
}
function init( ) {
var button1 = document.getElementById("btn1");
button1.addEventListener("click", computeScore);
}
window.addEventListener("load", init);
==================================================