Problem when repeating function with javascript in FOR cycle

1

Hello everyone I have a question I have this code

    for (i=1; i>=variable; i++ ){
       function formula'+i+'(){

       }
    }

I would like to know how I make the function repeat itself automatically I add the variable i but it tells me error

SyntaxError: missing (before formal parameters

I thank you in advance for the help provided

    
asked by Johendry Parra 04.08.2018 в 17:05
source

3 answers

1

I commented the following to how I understood your exercise, it should be as follows

let tope = 10
    for(i=0;i<=tope;i++){
      function saludo(){
        console.log('Hola soy el numero: ${i}')
      }
      saludo()
    }

In the example I use, I declare the following:

  • variable tope is going to be the limit that respects the for
  • said stop variable used in the for
  • inside the for I declare a function that will be responsible for printing the value of the variable i for each iteration made by for , I used template strings to print the value in question
  • just before the close of for I invoke the execution of the mode function saludo()
  •   

    For each iteration of the for from 0 to 10 in my example; the function is going   to be repeating and therefore executing the functionality that has   assigned

        
    answered by 04.08.2018 / 17:21
    source
    1

    I had already committed that what you are trying to do is not, it is self-generated code, which you would do with $php but if you only want in javascript you can achieve it with jQuery in this way:

    <html>
    <head>
    	<meta charset="UTF-8">
    	<title>Document</title>
    </head>
    <body>
    	<input type="submit" onclick="generar()" value="Autogenerar" />
    	<div id="btn">
    		
    	</div>
    	<div id="con">
    		
    	</div>
    	<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    	<script>
    		
    		function generar() {
    			var v="<script>";
    			for (var i = 5 ; i >= 0; i--) {
    				var a=$("#btn");				
    				a.append("<input type='submit' value='boton"+i+"' onclick='click"+i+"()'/>");
    				v=v+"function click"+i+"(){alert('Formula"+i+"');}";
    			}
    			var b=$("#con");
    			var v=v+"<\/script>";
    			b.append(v);
    		}	
    
    	</script>
    </body>
    </html>
        
    answered by 04.08.2018 в 17:55
    0

    To perform this operation, you must use bracket notation.

    for(i=1;i<11;i++) {
    	window['saludo' + i] = function(){alert('Hola!')};
    }
    
    console.log(saludo1);
    console.log(saludo2);
    console.log(saludo3);
    console.log(saludo4);
    console.log(saludo5);
    console.log(saludo6);
    console.log(saludo7);
    console.log(saludo8);
    console.log(saludo9);
    console.log(saludo10);
    // Correcto
     window['myvar' + num] = myValue; 
    
    // Incorrecto
     window.myvar + num = myValue; // SyntaxError: invalid assignment left-hand
    
        
    answered by 04.08.2018 в 17:55