2

I'm trying to learn JavaScript through .html files in Visual Studio 2013.

I know there are easier ways to reverse a string in JavaScript, but my main aim is to understand interaction between html and JavaScript to provide such a solution.

Also, the IDE has to be Visual Studio as eventually I want to code this same logic using .aspx files.

So, for my initial learning attempt, I'm trying to implement the following logic:

  • get a string from input tag of form,
  • submit form resulting in the script submit() to run,
  • the script reverses the string, and then
  • displays reversed string in the same input tag which is now disabled

Code I've written is as follows:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <script type="text/javascript">
        function reverseString() {
            var s = document.getElementById('reverseString').value;
            var reversed;

            for (var i = s.length - 1; i >= 0; i--) {
                reversed += s[i];
            }

            document.getElementById('reverseString').disabled = true;
            document.getElementById('reverseString').value = reversed;
        }

        function submit(form) {
            reverseString();
        }
    </script>

    <title>Play with Code</title>
</head>

<body>
    <form name="myform" onsubmit="submit();">
        Reverse String: <input type="text" id="reverseString"/><br/>
        <input type="submit" value="Submit"/>
    </form> 
</body>
</html>

It doesn't work and I'm really clueless as to how to solve it.

I unsuccessfully tried to debug following the instructions provided by these links:

How to debug (only) JavaScript in Visual Studio?

http://www.aspsnippets.com/Articles/Debug-JavaScript-and-jQuery-using-Visual-Studio-in-Internet-Explorer-browser.aspx

Please help me to fix this code and also, if possible, please advise or direct me on how to debug such a piece of code.

Thanks.

6
  • 5
    reverse = s.split('').reverse().join('') Commented Sep 17, 2015 at 8:36
  • Try var reversed = ''; Commented Sep 17, 2015 at 8:39
  • some info stackoverflow.com/questions/31167222/… Commented Sep 17, 2015 at 8:39
  • 1
    Don't ask two (and more) questions in one post Commented Sep 17, 2015 at 8:42
  • 1
    A note for everyone: Be careful naively reversing strings in JavaScript, as character encoding will bite you. Please read one of the best SO answers ever written on the topic: stackoverflow.com/a/16776621/2505965 Commented Sep 17, 2015 at 8:49

2 Answers 2

1

Here's a working code with comments to explain you what you are doing wrong:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <title>Play with Code</title>
</head>
<body>
    <form name="myform" id="form">
        Reverse String: <input type="text" id="reverseString"/><br/>
        <input type="submit" value="Submit"/>
    </form>
    <script>
        function reverseString() {
            var s = document.getElementById('reverseString').value;
            // reverse should initialized as a empty String
            // to prevent adding char to "undefined" string
            var reversed = '';

            for (var i = s.length - 1; i >= 0; i--) {
                reversed += s[i];
            }

            document.getElementById('reverseString').disabled = true;
            document.getElementById('reverseString').value = reversed;
        }

        function submit(ev) {
            // preventDefault prevent the form to do his automatic
            // behavior which submit the form with a new HTTP request
            ev.preventDefault();
            reverseString();
        }

        // Attach the event to the form
        document.getElementById('form').addEventListener('submit', submit);
    </script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

Working Demo

More Info on Debugging

//HTML
Reverse String: <input type="text" id="reverseString"/><br/>
    <input type="button" value="Submit" onClick="funcreverse()"/>

//Script
 function funcreverse () {
        var s = document.getElementById('reverseString').value;
        var reversed = '';
        for (var i = s.length - 1; i >= 0; i--) {
            reversed += s[i];
        }

        document.getElementById('reverseString').disabled = true;
        document.getElementById('reverseString').value = reversed;
    };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.