Jump to content

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

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"

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]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.