Below is the script I created to get several things done, one after another. It is supposed to:
- Spin a round icon onload. (It will be onclick, but I haven’t incorporated that yet, but I know how to.)
- Pick a random number from 1-21 inclusive. For some reason, console.log displays two numbers, each on its own line. So there’s something wrong right there.
- Connect each number with the ID of a card on the page, and choose the ID of the card that matches the random number. Save that number in localStorage.
- Jump to the ID and color the font a contrasting color so it briefly stands out. Two error messages in the console are given (below). Don’t know what steps to take to fix them.
<script>
// spin the wheel
const myTimeout = setTimeout(jumpToRandom, 3000); // give wheel time to spin before doing rest of code
const icon = document.getElementById('icon');
icon.addEventListener('click', () => {
icon.classList.add('spin');
});
icon.addEventListener('animationend', ()=>{
icon.classList.remove('spin');
});
// make a random choice and slide the screen to display the choice
// save the choice to storage for use in the next function
icon.addEventListener("click", jumpToRandom);
function jumpToRandom() {
let choice = Math.floor(Math.random() * 20) + 1;
console.log("Random choice number is " + choice); // Why are two different numbers displayed?
switch (choice) {
case 1:
"window.location.assign('#gooddeeds')"; localStorage.setItem("choice", gooddeeds); getAttn();
break;
case 2:
"window.location.assign('#doesgotwant')"; localStorage.setItem("choice", doesgotwant); getAttn();
break;
case 3:
"window.location.assign('#doesjesusforgive')"; localStorage.setItem("choice", doesjesusforgive); getAttn();
break;
case 4:
"window.location.assign('#doyouhaveeternal')"; localStorage.setItem("choice", doyouhaveeternal); getAttn();
break;
case 5:
"window.location.assign('#howdoyouget')"; localStorage.setItem("choice", howdoyouget); getAttn();
break;
case 6:
"window.location.assign('#ifyouwere')"; localStorage.setItem("choice", ifyouwere); getAttn();
break;
case 7:
"window.location.assign('#iseveryone')"; localStorage.setItem("choice", iseveryone); getAttn();
break;
case 8:
"window.location.assign('#isjesusdead')"; localStorage.setItem("choice", isjesusdead); getAttn();
break;
case 9:
"window.location.assign('#isjesusthe')"; localStorage.setItem("choice", isjesusthe); getAttn();
break;
case 10:
"window.location.assign('#iamthe')"; localStorage.setItem("choice", iamthe); getAttn();
break;
case 11:
"window.location.assign('#name3of')"; localStorage.setItem("choice", name3of); getAttn();
break;
case 12:
"window.location.assign('#recitejohn')"; localStorage.setItem("choice", recitejohn); getAttn();
break;
case 13:
"window.location.assign('#namesofjesus')"; localStorage.setItem("choice", namesofjesus); getAttn();
break;
case 14:
"window.location.assign('#whatdidjesusdo')"; localStorage.setItem("choice", whatdidjesusdo); getAttn();
break;
case 15:
"window.location.assign('#bornagain')"; localStorage.setItem("choice", bornagain); getAttn();
break;
case 16:
"window.location.assign('#comeback')"; localStorage.setItem("choice", comeback); getAttn();
break;
case 17:
"window.location.assign('#enterheaven')"; localStorage.setItem("choice", enterheaven); getAttn();
break;
case 18:
"window.location.assign('#havetodie')"; localStorage.setItem("choice", havetodie); getAttn();
break;
case 19:
"window.location.assign('#needsaving')"; localStorage.setItem("choice", needsaving); getAttn();
break;
case 20:
"window.location.assign('#lambofgod')"; localStorage.setItem("choice", lambofgod); getAttn();
break;
case 21:
"window.location.assign('#bigdeal')"; localStorage.setItem("choice", bigdeal); getAttn();
break;
}
// grab the choice from storage
function getAttn() {
var elem = localStorage.getItem("choice");
alert("elem is " + elem); // console: "elem is [object HTMLDivElement]"
// select the ID of the choice and change the font color to highlight the choice
var elem2 = document.getElementById('elem'); // console says "Uncaught TypeError: elem2 is null"
alert("elem2 is " + elem2);
elem2.style.color = 'green';
}
}
</script>