Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem creating a for-loop using Javascript. It seems everyting is fine for me but still I didn't get what I want.

Take a look please to this code to understand more:

  • The HTML form code:

    <form name="myform">
        <textarea name="inputtext" cols="100%" rows="10%"></textarea><br />
        <input type="radio" name="options" value="javascript" checked> Option1 <br />
        <input type="radio" name="options" value="windows"> Option2<br />
        <input type="button" value="Do it" onClick="generate();"><br />
        <textarea name="outputtext" cols="100%" rows="10%"></textarea><br />
    </form>
    
  • The Javascript code:

    function generate() {
    var code = ""+document.myform.inputtext.value;
    if (document.myform.options[0].checked) {
        document.myform.outputtext.value = escape(code);
    }
    else {
        var result= "2- ";
        for(int i=0; i<code.length; i++) {
        //There will be some logic to decide if to add the char or not.
        result+=code.charAt(i);
        }
        document.myform.outputtext.value = result;
    }
    }
    

The problem is not clear for me. However, when I try to comment out the for-loop, everything works fine !

Any ideas?

share|improve this question
    
int is not a datatype in javascript. Please use a javascript debugger (like firebug on firefox) to help you out. –  Munim May 15 '11 at 19:26
add comment

2 Answers

up vote 8 down vote accepted

There is no int data type in Javascript (or any data types at all used to declare variables).

for(var i=0; i<code.length; i++) {
share|improve this answer
    
OMG! How I miss that ! Sometimes you mix between Java and Javascript. Thanks @Guffa :) –  iturki May 15 '11 at 19:29
    
@2rk: I have the same problem jumping between C# and Javascript. I have done the exact same error several times. :) –  Guffa May 15 '11 at 20:20
add comment

There is also an Object-oriented solution to this.

var generate = {
   loop: function() {
        var code = ""+document.myform.inputtext.value;

        if (document.myform.options[0].checked) {
            document.myform.outputtext.value = escape(code);
        }
        else {
            var result= "2- ";
            //CHANGE THE INT(I assume Java) DATATYPE TO A LOCAL VARIABLE USING THE var KEYWORD TO KEEP THE SCOPE IN THE FOR LOOP
            //RECURSION CAN BE QUICKER
            for(var i=0; i<code.length; i++) {
            //There will be some logic to decide if to add the char or not.
            result+=code.charAt(i);
        }
        document.myform.outputtext.value = result;
     }
 }

generate.loop();
share|improve this answer
    
You forgot the closing bracket for the loop. –  Guffa May 15 '11 at 19:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.