In: Computer Science
Write a program that uses an array for time and
temperature. The program should contain an array with 24 elements,
each of which is holding a temperature for a single hour.
Your program should have a function that lists all of
the temperatures that are held in the array. Temperatures should be
listed to console with one entry per line.
Your program should have a function that lists a
single temperature entry. You should ask the user for a number and
list that entry from newest to oldest. For example if the user
enters 5, then you should show the entry from 5 hours
ago.
Your program should have a function that adds an entry
to the array and shifts the oldest entry out of the array. (You
must keep 24 entries.)
Your program should ask the user if they want to list all of the entries, list a single entry, or enter a new entry. Your program should continue to loop until the user clicks the cancel button OR clicks the enter button without entering a choice.
use let
And console.log for the coding
Javascript language console.log and use let
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//for testing
//let temp = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24];
let temp=[]
function AddEntry() {
let x = prompt("Enter Temperature:");
temp.unshift(x);
if (temp.length > 24)
temp.pop();
}
function SingleEntry() {
num = prompt("Enter Number");
let temp_at = temp[parseInt(num) - 1];
console.log("Temperature :", temp_at);
}
function ListAllEntry() {
for (let i = 0; i < temp.length; i++)
console.log(temp[i]);
}
let loop = true;
while (loop) {
let choice = prompt("Enter Choice :\n1.Show all Entry\n2.Add temp\n3.Show Single Entry\n")
if (choice === null) {
loop = false;
break;
}
// console.log(choice," ",typeof(choice));
switch (choice) {
case "1":
ListAllEntry();
break;
case "2":
AddEntry();
break;
case "3":
SingleEntry();
break;
}
}
</script>
</body>
</html>