generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

JavaScript help

Previous Thread :: Next Thread 
SoulStealer9875 is not online. SoulStealer9875
Joined: 20 Jul 2009
Total Posts: 13119
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
SoulStealer9875 is not online. SoulStealer9875
Joined: 20 Jul 2009
Total Posts: 13119
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 is not online. Xre
Joined: 26 Jul 2010
Total Posts: 670
17 Jul 2011 03:28 AM
delte ur system32
Report Abuse
SoulStealer9875 is not online. SoulStealer9875
Joined: 20 Jul 2009
Total Posts: 13119
17 Jul 2011 03:35 AM
@xre can you go away?
Report Abuse
burgly is not online. burgly
Top 100 Poster
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
SoulStealer9875 is not online. SoulStealer9875
Joined: 20 Jul 2009
Total Posts: 13119
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 is not online. burgly
Top 100 Poster
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
SoulStealer9875 is not online. SoulStealer9875
Joined: 20 Jul 2009
Total Posts: 13119
17 Jul 2011 03:41 AM
@burgly, because the wiki is based on Lua not JavaScript.
Report Abuse
SoulStealer9875 is not online. SoulStealer9875
Joined: 20 Jul 2009
Total Posts: 13119
17 Jul 2011 03:41 AM
What was the point of removing the function?
Report Abuse
SoulStealer9875 is not online. SoulStealer9875
Joined: 20 Jul 2009
Total Posts: 13119
17 Jul 2011 03:42 AM
Nevermind, I noticed I copied the wrong part.

floodcheck = "noob"
Report Abuse
SoulStealer9875 is not online. SoulStealer9875
Joined: 20 Jul 2009
Total Posts: 13119
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 is not online. burgly
Top 100 Poster
Joined: 20 Aug 2006
Total Posts: 2843
17 Jul 2011 03:47 AM
What happens with this version?
Report Abuse
Xre is not online. Xre
Joined: 26 Jul 2010
Total Posts: 670
17 Jul 2011 03:48 AM
Stuff.
Report Abuse
SoulStealer9875 is not online. SoulStealer9875
Joined: 20 Jul 2009
Total Posts: 13119
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 is not online. HatHelper
Top 25 Poster
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 is not online. Xre
Joined: 26 Jul 2010
Total Posts: 670
17 Jul 2011 03:50 AM
k
Report Abuse
burgly is not online. burgly
Top 100 Poster
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
SoulStealer9875 is not online. SoulStealer9875
Joined: 20 Jul 2009
Total Posts: 13119
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 is not online. burgly
Top 100 Poster
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
SoulStealer9875 is not online. SoulStealer9875
Joined: 20 Jul 2009
Total Posts: 13119
17 Jul 2011 03:54 AM
It works now, thanks.
Report Abuse
burgly is not online. burgly
Top 100 Poster
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
SoulStealer9875 is not online. SoulStealer9875
Joined: 20 Jul 2009
Total Posts: 13119
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 is not online. HatHelper
Top 25 Poster
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
SoulStealer9875 is not online. SoulStealer9875
Joined: 20 Jul 2009
Total Posts: 13119
17 Jul 2011 04:19 AM
Lol...
Report Abuse
NXTBoy is not online. 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
Previous Thread :: Next Thread 
Page 1 of 2Go to page: [1], 2 Next
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image