I’m creating a game where a user is presented with an English word, and i want the user to fill in the same word in Swedish in the box right next to the English word.
I have made two arrayas, one with all the English words, and one with Swedish words.
The correct answer to array items number 4 in first array, is the one with number 4 in the other array and so on.
But now i’m not sure how i can compare if what the user types in the box is the same as the word presented to them. The English word that appears is taken random from anywhere in the array.
One way would of course be a lot of if statements but thats not how i would like to go about things due to the fact that i later will try to add a function so that the user could add their own words with the result.
I’m sorry if the question is a bit confusing, i will answer any questions if you intend to help me, thanks a lot in advance!
Well the problem is I’m new to Javascript and i currently have brain collapse ;).
I have 3 variables
var glosorEng = ["World", "Country", "Cat", "Dog", "Computer", "Fire", "Mousepad", "Math", "Russia"]
var glosorSV = ["Värld", "Land", "Katt", "Hund", "Dator", "Eld", "Musmatta", "Matematik", "Ryssland",]
var randomGlosa = Math.floor(Math.random() * glosorEng.length);
And a function “add” and it will randomly take an English word out of the Array and type it in a DIV container. Lets say the word is “Fire”, and the user types “Eld” which is the correct answer in this case.
You are comparing two strings from two different languages, so they will definitely not be equal. What you need to compare is the value entered by the user to glosorSV[randomGlosa]
randomGlosa represents the index. You are comparing the element of two arrays having the same index. Let’s say randomGlosa is equal to 2. That means the element of the glosorEng array would be “Cat” and the element of the glosorSV array would be “Katt”. Your if statement is checking if “Cat” is equal to “Katt”. They are not equal.
Hey again. I’m not sure why it says solved, not sure how i did that by accident if it was me, anyhow its not solved. I know i can’t check if the words are the same because they are both in a different languages. So far i’m understanding. I also understand that i somehow have to check if the values.
However i do not understand how to do this, even though I’m pretty sure its very easy and basic. Here is a link to codepen, thanks!
ps This is a school project, so i’m 100% interessted in learning, not getting code from somebody else, also this forum will be added in the code as a comment from where i got the idea.
The reason it did not work is because of two problems with your code:
To get the value of an input element, you do not use innerHTML. Do some research on how to get the value of an input element.
Once you figure out #1 above, the next thing you need to think about is where/when should you actually get the value for userEnter. Currently, you assign a value to it when the page loads. The problem is, when the page loads, the user has not yet typed anything into the textbox. You need to think about where it makes sense to capture the value and assign it to userEnter.
EDIT : I’ve made it work!! Thanks for helping know how to think regarding solving the issue, thanks again!!
Hey again, thanks for the quick reply
To get the value of an input element, you do not use innerHTML. Do some research on how to get the value of an input element.
Ops my bad, it should of course say .value not innerHTML
Once you figure out #1 above, the next thing you need to think about is where/when should you actually get the value for userEnter. Currently, you assign a value to it when the page loads. The problem is, when the page loads, the user has not yet typed anything into the textbox. You need to think about where it makes sense to capture the value and assign it to userEnter.
This seems very logical, I’ve moved the var that takes the value of what the user has typed inside of the “checkAnswer” function, so now when that function is called upon the user has actually typed something.
So i think i have solved the 2 problems you were pointing out, or am i misstaken?