Jump to content

code is not working? why?


newbe123

Recommended Posts

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> 

Link to comment
https://forums.phpfreaks.com/topic/221238-code-is-not-working-why/
Share on other sites

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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.