newbe123 Posted December 10, 2010 Share Posted December 10, 2010 Why is this not working? This is what it does: you can type something into the first box, such as your name. Click on the button. This goes through the string one character at a time. .charAt (..) picks up a single character from the string. It then adds it to the front of the output string. This has the effect of reversing it. When you code a loop, you need to know how many times, and in this case it's the number of characters in the string, in other words the string length or xx.length. <HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Reverse string function reverse() { var inp = document.rev.inpt.value var outp = 0 for (i = 0; i <= inp.length; i++) { outp = inp.charAt (i) + outp } document.reals.outpt.value = outp } </SCRIPT> </HEAD> <BODY> <FORM ACTION="#" NAME="rev"> <INPUT TYPE=TEXT NAME="inpt" SIZE=20 > <INPUT TYPE=BUTTON VALUE="Reverse" ONCLICK="reverse()"><BR> <INPUT TYPE=TEXT NAME="outpt" SIZE=20 DISABLED> </FORM> </BODY> </HTML> Quote Link to comment https://forums.phpfreaks.com/topic/221238-code-is-not-working-why/ Share on other sites More sharing options...
n3r0x Posted December 11, 2010 Share Posted December 11, 2010 You have 2 problems, 1. var outp = 0 should be var outp = '' or your script will return the string reversed and a extra 0 in the end 2. document.reals.outpt.value = outp should be document.rev.outpt.value = outp since there are no form with the name "reals" Quote Link to comment https://forums.phpfreaks.com/topic/221238-code-is-not-working-why/#findComment-1145616 Share on other sites More sharing options...
newbe123 Posted December 11, 2010 Author Share Posted December 11, 2010 thank you so much. Quote Link to comment https://forums.phpfreaks.com/topic/221238-code-is-not-working-why/#findComment-1145620 Share on other sites More sharing options...
amir_polo2003 Posted December 11, 2010 Share Posted December 11, 2010 this code can work true. red parts must be changed .. ! <HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Reverse string function reverse() { var inp = document.rev.inpt.value var outp = "" for (i = 0; i <= inp.length; i++) { outp = inp.charAt (i) + outp } document.rev.outpt.value = outp } </SCRIPT> <FORM ACTION="#" NAME="rev"> <INPUT TYPE=TEXT NAME="inpt" SIZE=20> <INPUT TYPE=BUTTON VALUE="Reverse" ONCLICK="reverse()"><BR> <INPUT TYPE=TEXT NAME="outpt" SIZE=20 DISABLED> </FORM> </BODY> </HTML> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/221238-code-is-not-working-why/#findComment-1145622 Share on other sites More sharing options...
desjardins2010 Posted December 11, 2010 Share Posted December 11, 2010 isnt' that what r0x just said?? Quote Link to comment https://forums.phpfreaks.com/topic/221238-code-is-not-working-why/#findComment-1145628 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.