简体   繁体   English

使用Java创建for循环?

[英]creating a for-loop using Javascript?

I have a problem creating a for-loop using Javascript. 我在使用Javascript创建for循环时遇到问题。 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: HTML表单代码:

     <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: Javascript代码:

     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 ! 但是,当我尝试注释掉for循环时,一切正常!

Any ideas? 有任何想法吗?

Javascript中没有int数据类型(或所有用于声明变量的数据类型)。

for(var i=0; i<code.length; i++) {

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM