Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Game for Kids

I'm trying to make a game in javascript. I still have many issues in the code. The code is supposed to have 2 buttons; one for shuffling images dice and a button to check if the kid wrote the correct answer or not.

  • 2 buttons
  • 1 textbox
  • and a place to show the answer if it's correct or not

Here is my code.

I don't know why when I put the source of document.getElementbyID("test") it shows nothing because I want every time I click on start a random image is selected.

I would appreciate any help as I am still a beginner in javascript.

  <head> 

<script type="text/javascript">


function startf(){
var images = []; 
index = 0;
images[0] = "<img src='1.png' length=70px width=75px>";
images[1] = "<img src='2.png' length=70px width=75px>";
images[2] = "<img src='3.png' length=70px width=75px>";
images[3] = "<img src='4.png' length=70px width=75px>";
index = Math.floor(Math.random() * images.length);
take(index);

function take(ind)
{
return document.getElementbyId("ind")="What should i put here";
}
}
function check(){
var ch=getElementbyId("answer").value;
if (ch=index+1)
{
document.getElementbyId.innerHTML="TRUE";
}
else
{
document.getElementbyId.innerHTML="False";
}

}
</script><br>
</head>
<img id="test" src=""  alt="..." length="75px" width="75px" />

<body>

<input type="button" value="start" onclick="startf()">
<input id="answer" type="text" name="checkvalue" value="Enter Value" onclick="check()">
<div id="fa">
</div>
<input type="button" value=chek onclick="check()">


</body>
like image 777
user1928775 Avatar asked Mar 13 '26 22:03

user1928775


1 Answers

1- Put and end to each instructions --> ;

2- Do not use document.getElementById directly, there will be at least one occurence with an error into it and you don't want that.

function _e(id) {
   return document.getElementById(id);
}

3- Always put brackets and (...) around your IF-ELSE blocks:

if (...) {
    //....
} else {
    //....
}

4- Every tag attributes should have " " around their values, for example:

<input type="button" value="start" onclick="check()" />

5- You could only put image paths in your array since it seems to be what needs to be updated in #test image.

like image 84
Frederik.L Avatar answered Mar 15 '26 10:03

Frederik.L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!