I know this is silly, but as a little joke based on a conversation with family, I made this:
var hungerLevel;
var breakTime;
function shouldIEat(hungerLevel, breakTime) {
if (hungerLevel > 5 || breakTime > 90) {
alert("Eat!");
} else {
alert("Do not eat.");
}
}
If I were to actually create a web page with inputs for Hunger Level and Length of break, how would I attach the function to the inputs? Is this something I can figure out before finishing the Basic Javascript challenges? And could I do this on Codepen? Or do I need to use some sort of hosting so that the data actually gets collected?
Yes, you can, but it’s a bit of a long stretch probably. Essentially you could:
Write an <input type="text"/> for each value
Execute the function when the person fills both, something like this
<input type="text" id="inputA" required/>
<input type="text" id="inputB" required/>
<button onclick="shouldIEat()">Go</button>
<script>
//Your function here
</script>
But that won’t work yet. You still need to gather the input values, which is something like this: let inputA = document.getElementById("inputA").value
and same for the other element.
If this looks to complicated, try to create a function that writes some innerHTML, it could be just a word. You can find examples almost anywhere.
This is a nice exercise, it’s easier, and goes along better with the course I believe.
Thanks for all the guidance and links. I’ll give it a go. It’s certainly not super useful, but it will teach me how JavaScript really works in websites.