|
| 17 Jul 2011 03:21 AM |
I'm pretty new to javascript and I need help with this function;
function checkAnswers() { var a=document.forms["yo"]["a"].value var b=document.forms["yo"]["b"].value var c=document.forms["yo"]["c"].value var amnt = 0; if (a value=="4"); { amnt + 1; return true; } if (b value=="5"); { amnt + 1; return true; } if (c value=="7"); { amnt + 1; return true; } document.write("You got "+amnt+"/3 correct."); } |
|
|
| Report Abuse |
|
|
|
| 17 Jul 2011 03:23 AM |
Well, I fixed it a bit. Now if a's value is 4, it still says 'You got 0/3 correct.'. Changed document.write to alert:
function checkAnswers() { var a=document.forms["yo"]["a"].value var b=document.forms["yo"]["b"].value var c=document.forms["yo"]["c"].value var amnt = 0; if (a=="4"); { amnt + 1; } if (b=="5"); { amnt + 1; } if (c=="7"); { amnt + 1; } alert("You got "+amnt+"/3 correct."); } |
|
|
| Report Abuse |
|
|
Xre
|
  |
| Joined: 26 Jul 2010 |
| Total Posts: 670 |
|
| |
|
| |
|
burgly
|
  |
 |
| Joined: 20 Aug 2006 |
| Total Posts: 2843 |
|
|
| 17 Jul 2011 03:36 AM |
You never change the value of Amt... Amt = Amt + 1 |
|
|
| Report Abuse |
|
|
|
| 17 Jul 2011 03:38 AM |
Now it says I have 3/3 when I avoid the correct answers:
function checkAnswers() { var a=document.forms["yo"]["a"] var b=document.forms["yo"]["b"] var c=document.forms["yo"]["c"] var amnt=0; if (a.value=="4"); { amnta = amnt + 1; } if (b.value=="5"); { amntb = amnt + 1; } if (c.value=="7"); { amntc = amnt + 1; } alert("You got "+(amnta+amntb+amntc)+"/3 correct."); } |
|
|
| Report Abuse |
|
|
burgly
|
  |
 |
| Joined: 20 Aug 2006 |
| Total Posts: 2843 |
|
|
| 17 Jul 2011 03:39 AM |
No offense, but how are you Wiki Staff? lol function checkAnswers() { var a=document.forms["yo"]["a"] var b=document.forms["yo"]["b"] var c=document.forms["yo"]["c"] var amnt=0; if (a.value=="4"); { amnt = amnt + 1; } if (b.value=="5"); { amnt = amnt + 1; } if (c.value=="7"); { amnt = amnt + 1; } alert("You got "+amnt+"/3 correct."); } |
|
|
| Report Abuse |
|
|
|
| 17 Jul 2011 03:41 AM |
| @burgly, because the wiki is based on Lua not JavaScript. |
|
|
| Report Abuse |
|
|
|
| 17 Jul 2011 03:41 AM |
| What was the point of removing the function? |
|
|
| Report Abuse |
|
|
|
| 17 Jul 2011 03:42 AM |
Nevermind, I noticed I copied the wrong part.
floodcheck = "noob" |
|
|
| Report Abuse |
|
|
|
| 17 Jul 2011 03:43 AM |
Still doesn't work, whole script:
< script type="text/javascript"> function checkAnswers() { var a=document.forms["yo"]["a"] var b=document.forms["yo"]["b"] var c=document.forms["yo"]["c"] var amnt=0; if (a.value=="4"); { amnt = amnt + 1; } if (b.value=="5"); { amnt = amnt + 1; } if (c.value=="7"); { amnt = amnt + 1; } alert("You got "+amnt+"/3 correct."); } < /script> < form name="yo"> 2 ^ 2 = < input type="textfield" name="a" value="">< br> 3 + (4/2) = < input type="textfield" name="b" value="">< br> 1 -- 6 = < input type="textfield" name="c" value="">< br> < input type="button" value="Submit" onclick="checkAnswers()"> < /form> |
|
|
| Report Abuse |
|
|
burgly
|
  |
 |
| Joined: 20 Aug 2006 |
| Total Posts: 2843 |
|
|
| 17 Jul 2011 03:47 AM |
| What happens with this version? |
|
|
| Report Abuse |
|
|
Xre
|
  |
| Joined: 26 Jul 2010 |
| Total Posts: 670 |
|
| |
|
|
| 17 Jul 2011 03:48 AM |
...
It's meant to be three textboxes asking math questions and if you type in the correct answers an alert pops up saying you got (amount of questions you got right)/3. |
|
|
| Report Abuse |
|
|
HatHelper
|
  |
 |
| Joined: 02 Mar 2009 |
| Total Posts: 46305 |
|
|
| 17 Jul 2011 03:49 AM |
| burgly, I had that same Q. no offense soulguy |
|
|
| Report Abuse |
|
|
Xre
|
  |
| Joined: 26 Jul 2010 |
| Total Posts: 670 |
|
| |
|
burgly
|
  |
 |
| Joined: 20 Aug 2006 |
| Total Posts: 2843 |
|
|
| 17 Jul 2011 03:50 AM |
I mean what's wrong with it? Like how "Now if a's value is 4, it still says 'You got 0/3 correct.'" I don't have everything, so I'm not able to test your code. I visualize it the best I can, but nothing beats the computer. |
|
|
| Report Abuse |
|
|
|
| 17 Jul 2011 03:51 AM |
| 'a' is a textfield. If you type in '4' in that textfield and press 'Submit', the function is fired and a message pops up saying "You got 0/3" when it should say "You got 1/3". |
|
|
| Report Abuse |
|
|
burgly
|
  |
 |
| Joined: 20 Aug 2006 |
| Total Posts: 2843 |
|
|
| 17 Jul 2011 03:53 AM |
'Try this:
< script type="text/javascript"> function checkAnswers() { var a=document.forms["yo"]["a"] var b=document.forms["yo"]["b"] var c=document.forms["yo"]["c"] var amnt=0; if (a.value=="4") { amnt = amnt + 1; } if (b.value=="5") { amnt = amnt + 1; } if (c.value=="7") { amnt = amnt + 1; } alert("You got "+amnt+"/3 correct."); } < /script> < form name="yo"> 2 ^ 2 = < input type="textfield" name="a" value="">< br> 3 + (4/2) = < input type="textfield" name="b" value="">< br> 1 -- 6 = < input type="textfield" name="c" value="">< br> < input type="button" value="Submit" onclick="checkAnswers()"> < /form> |
|
|
| Report Abuse |
|
|
| |
|
burgly
|
  |
 |
| Joined: 20 Aug 2006 |
| Total Posts: 2843 |
|
|
| 17 Jul 2011 03:58 AM |
No prob. The original error, adding incorrectly, was something you shouldn't have made. It's a simple error ;]
I can understand that you don't know about semicolons, seeing as they aren't used to end lines in Lua [They can be, but no one ever bothers], and you're probably new at Javascript. |
|
|
| Report Abuse |
|
|
|
| 17 Jul 2011 04:01 AM |
| Yes, I am very new to javascript. I started learning yesterday on w3schools. I want to master all website development languages muahahahaha. |
|
|
| Report Abuse |
|
|
HatHelper
|
  |
 |
| Joined: 02 Mar 2009 |
| Total Posts: 46305 |
|
|
| 17 Jul 2011 04:03 AM |
i only started learning js to send ppl at mah school to inappropriate sites
|
|
|
| Report Abuse |
|
|
| |
|
NXTBoy
|
  |
| Joined: 25 Aug 2008 |
| Total Posts: 4533 |
|
|
| 17 Jul 2011 04:30 AM |
Because of how javascript works, you can actually simplify it to this:
function checkAnswers() { var form = document.forms.yo; var correct = (form.a.value == 4) + (form.b.value == 5) + (form.c.value == 7); alert('You got ' + correct + '/3 correct.'); }
Booleans are converted to 0 or 1 when used with mathematical operators. Strings are converted to numbers when compared with numbers.
More importantly though, semi-colons are a VERY good idea in javascript. Code can be ambiguous otherwise. And it's good practice to use single quotes in javascript.
Finally, the type attribute on your inputs should be "text", not "textfield". |
|
|
| Report Abuse |
|
|