Tinkerers - JS Functions Popcorn Hack Solutions
Categories: JavaScriptThese are the solutions to the popcorn hacks.
🍿 POPCORN HACK NO. 1 🍿
Solution:
%%html
<html>
<body>
<p>Farenheight to Celsius Converter</p>
<input type="number" id="inputField" placeholder="Enter degrees (F)">
<button id="calculateButton">Calculate</button>
<div id="output1">Your results will appear here.</div>
<script>
(() => {
const button = document.getElementById("calculateButton");
const input = document.getElementById("inputField");
const output = document.getElementById("output1");
// ^^ DO NOT MODIFY ANY ABOVE CODE ^^
// Define your function out here
function toCelsius(degrees_fh){
return (degrees_fh - 32) / 1.8
}
let result;
let inpval;
button.addEventListener("click", () => {
/*
Call your toCelsius() function, using the user's input from input.value.
Save the result to a variable.
Note that input.value returns a string. You will need to turn it into a
number using Number(), and then feed that number to your function.
*/
inpval = Number(input.value);
result = toCelsius(inpval);
// Log the result to console
console.log(inpval + " F = " + result + "C");
// Add the result to the end of DOM:
output.innerText += "\n" + inpval + " F = " + result + "C"
// vv DO NOT MODIFY ANY BELOW CODE vv
});
})();
</script>
</body>
</html>
Farenheight to Celsius Converter
Your results will appear here.
🍿 POPCORN HACK NO. 2 🍿
Solution:
%%html
<div id="phack2">Your results will appear here:</div>
<script>
(() => {
const output = document.getElementById("phack2");
function findMax(arr){
let maxNum = arr[0];
for (let i = 0; i < arr.length; i++){
if (arr[i] > maxNum){
maxNum = arr[i];
}
}
output.innerText += "\n" + "Maximum number in array [" + arr + "] is " + maxNum;
console.log("Maximum number in array [" + arr + "] is " + maxNum);
}
findMax([1, 2, 3, 4, 4, 4, 5, 6, 8, 4, 1, 7]);
findMax([1]);
findMax([97883, 981, 81283, 987213, 1, -391]);
})();
</script>
Your results will appear here