Can you help out in this code work | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can you help out in this code work

dictionary index value I'm trying to get the code to say, if next button, ask next question index.value num1 = ["What is 3 x 6": "18", "What is 3 x 7": "21", "What is 3 x 8": "24"] var i = 0; function myFunc() { for(i =< 0) = num1.length; i++; if(sanserif.value == num1[i++]) { document.getElementById("monospace").innerHTML = "correcct"; } } function nextFunc() { document.getElementById("monospace").innerHTML = num1[i++] } https://code.sololearn.com/WT6RNLRf51xp/?ref=app

27th Dec 2021, 3:35 PM
Steve Nova
Steve Nova - avatar
20 Answers
+ 2
//Sorry, I had to modify a lot your code so that it would work like what you like and I improved it little : const num1 = {"What is 3 x 6": "18", "What is 3 x 7": "21", "What is 3 x 8": "24"}; let i = -1; function myFunc() { if(document.getElementById("sanserif").value == Object.values(num1)[i]) { document.getElementById("monospace").innerHTML = "correct"; } } function nextFunc() { if(i < Object.keys(num1).length - 1 && document.getElementById("monospace").innerHTML == "correct" || !document.getElementById("monospace").innerHTML) document.getElementById("monospace").innerHTML = Object.keys(num1)[++i] } nextFunc(); //If you need explanations, I am here, or you can follow me so that I will be able to help you in your codes or learn you with my tutorials which are coming soon
27th Dec 2021, 4:14 PM
VCoder
VCoder - avatar
+ 3
The code has to ask What is 3 x 6 18 Next( ) What is 3 x 7 21 Next( )
27th Dec 2021, 4:05 PM
Steve Nova
Steve Nova - avatar
+ 1
Please explain a little more what you want to do
27th Dec 2021, 3:56 PM
VCoder
VCoder - avatar
+ 1
Yes, and also your code will be faster if you access monospace, sanserif, cursive and the others by a variable (const monospace = document.getElementById("monospace"); //good) instead by document.getElementById() because JS will navigate only 1 time through the DOM to find them instead of more
28th Dec 2021, 5:37 PM
VCoder
VCoder - avatar
0
Wow it works, you really nailed it on this one Thanks a lot
27th Dec 2021, 4:34 PM
Steve Nova
Steve Nova - avatar
0
Thanks VCoder the [Math.floor(Math.random() * tryAgain.length)] works just fine, sorry I trying to reply but the messages are not sending on my phone. But anyways everything is fine now
28th Dec 2021, 5:15 PM
Steve Nova
Steve Nova - avatar
0
Replace the else if statement by an else one
28th Dec 2021, 5:23 PM
VCoder
VCoder - avatar
0
And you don't need to write +1, just 1
28th Dec 2021, 5:25 PM
VCoder
VCoder - avatar
0
Ok do you mean like this? else { document.getElementById("monospace").innerHTML = tryAgain[Math.floor(Math.random()*tryAgain.length]; }
28th Dec 2021, 5:30 PM
Steve Nova
Steve Nova - avatar
0
Ok cool But how do I hide the next() function thou until a correct answer is clicked
28th Dec 2021, 5:41 PM
Steve Nova
Steve Nova - avatar
0
You speak about the last line ?
28th Dec 2021, 5:43 PM
VCoder
VCoder - avatar
0
Yes
28th Dec 2021, 5:44 PM
Steve Nova
Steve Nova - avatar
0
Try to comment it and you will understand why I add it
28th Dec 2021, 5:45 PM
VCoder
VCoder - avatar
0
Ok I'll try now
28th Dec 2021, 5:47 PM
Steve Nova
Steve Nova - avatar
0
It doing nothing
28th Dec 2021, 5:50 PM
Steve Nova
Steve Nova - avatar
0
if(correct answer then next); is this a possible code
28th Dec 2021, 5:52 PM
Steve Nova
Steve Nova - avatar
0
Did you notice that the first question isn't displayed
28th Dec 2021, 5:52 PM
VCoder
VCoder - avatar
0
Yes
28th Dec 2021, 5:53 PM
Steve Nova
Steve Nova - avatar
0
1_close the parentheses of Math.floor() 2_don't worry, the last line is used only to display the first question
28th Dec 2021, 6:17 PM
VCoder
VCoder - avatar
0
Ok thanks
28th Dec 2021, 6:18 PM
Steve Nova
Steve Nova - avatar