Reputation: 41
I'm 3 months in to my education where i will learn different languages as HTML,CSS,JS and a lot more. At this moment I'm currently stuggling with finding a solution, mostly because im new and i'm not really sure what to look for. I'm creating a website (will not upload to internet, only show for teacher) where i have different types of small games. Just to show what i've learnt so far, and what i can do (we are allowed to search the web of course).
So i will use an example just for you to know what im dealing with, this how ever is not the exact same im doing in my project, but the function is.
I have 2 arrays. The first array contain 10 countries, the second array contains 10 capital cities. The capital on place 5 in the capital array, is the capital city of the country on place 5 in my coutry array.
And this goes for all options. I've also made a DIV tag where Math.random picks 1 country at random.... and displays it in the DIV container. And i got a input type="text" field for the user to type in the captial city for that country.
This is where it gets tricky for me, how can i check if what they wrote as a string, has the same index value (I think its called that?) as the other array? I'm totaly lost on how this is done.
I could of course use a lot of if statements, like if(userInput == Washington) then alert("Correct!").
However this is not how i want to do it, because i will later add an option for the user to add their own coutries/captials.
I really feel like this got messy, so please ask questions if you don't understand and i will try to break it down further, thanks for you patience, cheers!
Upvotes: 0
Views: 166
Reputation:
Since your array Country and array Capital have the same indexes (and I assume they will always have). You can use the Math.random function to generate a number. (Like you do)
You already know the answer, since Country[1] should have as answer Captial[1] So what you want to check if what they wrote equals your answer. And that is the thing you know.
So we want to know if the user input equals the Capital array at math.random index:
If(userinput === Captial[math.random number here]) //do your stuff
there are functions that can help you with checking the string. Because If you have "London" or "london" it might differ the correctness of the answer.
I think the most important thing, isn't the code. But the thinking processes.
__ Edit: Based on the code you provided there are a few things.
First of all, userEnter == glosorSV[ri]
the variable ri has not been defined. This has two reasons: the function where you want to generate a random number is never called, and even if you call it it doesn't return a number.
function randomizeWord(){
var ri = Math.floor(Math.random() * glosorEng.length);
document.getElementById("contentG").innerHTML = glosorEng[randomGlosa];
You have this function and it will create a random number. But then you take the variable randomGlosa
to pick an element in your array, while randomGlosa isn't defined. The variable that contains the random number is called ri, so you want to change this to glosorEng[ri]
or you want to change var ri
to var randomGlosa
The next problem is that this function doesn't give anything back. It's a void function. I have no clue how far you are with functions in your course. Void functions are used when there is no output that the 'main' program needs to process. In this case, we do care about since we need a random number. Besides, it doesn't have to be a function at all unless your assignment wants it to be a function. So we can define a variable that's available in the whole script or you should give it a return value. Based on how you call the function.
The for-loop you created has no purpose. You take a list and loop through each element, but you don't use an element at all. Beside the i=0
should be a declaration for i so var i=0
or let i = 0
. About var and let, you should look up let and const to declare variables unless your assignment and reading materials don't use it. Then be aware of another, newer, method of declaration exists.
If you remove the for-loop the return is not needed, as the function will automatically stop since your return doesn't have a value. It will do the same.
So how should it look like:
//Have your vars here:
var 1 = ...
var 2 = ...
var ri = 0;
function randomizeWord(){
ri = Math.floor(Math.random() * glosorEng.length);
document.getElementById("contentG").innerHTML = glosorEng[ri];
}
function checkAnswer(){
//Get user input
if(userEnter === glosorSV[ri])
//Logic here for the alerts
}
I assume the functions are being called by buttons with an onclick event.
Upvotes: 1