comparison - Comparing two functions in JavaScript -
i'm developing mobile app wife's 1st grade class can practice sight words. i'm novice javascript able pull off first objective take javascript array , extract random word it. second objective have user type in word see, click button , have word entered compared random word. attempted second function did not it. don't errors in console i'm bit lost on how working. appreciated me , great group of 1st graders. here code have far.
<!doctype html> <html> <body> <h1>practice spelling test</h1> <p id="aword"></p> <input id="yourturn"> <button onclick="myfunction()">new word</button> <button onclick="checkspelling()">check spelling</button> <p id="result"></p> <script> var sightword = ["hoof", "hook", "shook", "hood", "wood", "took", "book", "good", "food", "mood", "look"]; var yourturn = document.getelementbyid("yourturn").value; var aword = document.getelementbyid("aword").value; var checkword = (yourturn == aword)?"nice job!":"so close! try again!"; function myfunction() { var showword = sightword[math.floor((math.random()*10)+1)]; aword.innerhtml = showword; } function checkspelling(result) { document.getelementbyid("result").innerhtml=checkword; } </script> </body> </html>
you mixed value
, innerhtml
.
value
used input
, textarea
elements , innerhtml
other element
this code work you:
<!doctype html> <html> <body> <h1>practice spelling test</h1> <p id="aword"></p> <input id="yourturn"> <button onclick="myfunction()">new word</button> <button onclick="checkspelling()">check spelling</button> <p id="result"></p> <script> var sightword = ["hoof", "hook", "shook", "hood", "wood", "took", "book", "good", "food", "mood", "look"]; var yourturn = document.getelementbyid("yourturn"); var aword = document.getelementbyid("aword"); function myfunction() { var showword = sightword[math.floor((math.random()*10)+1)]; aword.innerhtml = showword; } function checkspelling(result) { var checkword = (yourturn.value == aword.innerhtml)?"nice job!":"so close! try again!"; document.getelementbyid("result").innerhtml=checkword; } </script> </body> </html>
see live code here: http://jsbin.com/ubofus/1/edit
Comments
Post a Comment