2

I want to make a program in javascript in which a person inputted the iteration count for a for loop(they could input x++, or y--), but I don't know if I am using the right method.

Here is my code:

   var x = prompt("iteration count")
   // x should equal something like, i++, or x--
   for(var i = 0; i < 10; x){
        document.write(i)
   }

But when the code ran the program kept crashing. Why is it crashing and how do I fix this? Please Help

Abhiraam Eranti
  • 350
  • 5
  • 19

5 Answers5

3

you need to parse the int value of x because it's a string and use it to increment i

var x = parseInt(prompt("iteration count"))

for (var i = 0; i < 10; i += x) {
  document.write(i)
}

EDIT :

based on the question edit and the comments, you can use eval(), but :

Do not ever use eval!

eval() is a dangerous function, which executes the code it's passed with the privileges of the caller.

So before you use it, read the MDN page and check : eval isnt evil it's just misunderstood

where there's this comment from Spudley :

From a security perspective, eval() is far more dangerous in a server environment, where code is expected to be fully trusted and hidden from the end user.

In a browser, the user could eval any code they wanted at any time simply by opening dev tools, so as a developer you can't get away with having anything on your client code that could be insecure against eval anyway.

to test the snippet below, type i++ in the prompt

var x = prompt("iteration count");

for (var i = 0; i < 10; eval(x)) {
  console.log(i)
}

an alternative to eval() would be new Function or check the answers here : Programatically setting third statement of for loop

Taki
  • 17,320
  • 4
  • 26
  • 47
  • As requested, `// x should equal something like, i++, or x--` your code doesnt meet this criteria – Isaac Jun 29 '18 at 04:07
  • @Isaac the question was edited after this answer, i edited it accordingly – Taki Jun 29 '18 at 06:11
  • Noted. downvote removed. As for the `new Function` part if you wished to know maybe can refer to my answer – Isaac Jun 29 '18 at 06:15
  • @Isaac ah i see, i didn't see your answer but maybe you would like to add a link to MDN and some description ( as it is a form of `eval()` ) – Taki Jun 29 '18 at 06:23
0

  
var input = 'i++';//Or whatever condition user passing in
var conditionProgramatically = () => new Function(input)() ;

for (var i = 0; i < 10; conditionProgramatically()) {
  console.log(i)
}

For for-loop, third statement will be invoked/executed on every iteration, and hence we set a function call, and in that function, we execute whatever user passing in as you've mentioned i++

Isaac
  • 12,042
  • 16
  • 52
  • 116
  • not me but probably related to : https://stackoverflow.com/questions/51094172/programatically-setting-third-statement-of-for-loop – Taki Jun 29 '18 at 06:29
  • @Taki: What do you mean in exact because I was the person who posted the question lol – Isaac Jun 29 '18 at 06:30
  • @Taki: hmmm true. Weird enough that it works here, you can try "Run code snippet" but not on `JSFiddle`. Let me try to check see if manage to find out the difference – Isaac Jun 29 '18 at 06:49
  • here `var input = 'i++';` the `input` is harcoded but in the fiddle the `input` is read from the `prompt` – Taki Jun 29 '18 at 07:02
  • @Taki: I tried to hardcode `var input = 'i++'` at your `fiddle`, doesnt work too. – Isaac Jun 29 '18 at 07:04
  • Your code doesn't work with a local `i` variable - functions created by `Function` close over the global scope only. Also you should simplify (and optimise) to `conditionProgramatically = new Function(input)` – Bergi Jun 29 '18 at 07:41
  • @Bergi: But how to explain that running the code snippet here giving the expected result. Aren't they using the exact same JS Engine depending on browser? – Isaac Jun 29 '18 at 09:21
  • 1
    @Isaac [jsFiddle code by default does not run in a global script context](https://stackoverflow.com/q/5468350/1048572) – Bergi Jun 29 '18 at 09:45
-1

That is an endless loop because the variable i never incremented. Try this one.

var x = prompt("iteration count")
for(var i = 0; i < x, i++){
    document.write(i)
}
Joven28
  • 769
  • 3
  • 12
-1

You forgot to increment the index variable, it result to endless loop and maximum stack error, you can also use + for parseInt shorcut.

 var x = +prompt("iteration count")
 for(var i = 0; i < x;i++){
    document.write(i)
 }
onecompileman
  • 900
  • 7
  • 14
-1

You have to parse the input value and then make it as a condition to stop iterating after the given value.

var x = parseInt(prompt("iteration count"))

for (var i = 0; i < x; i++) {
  document.write(i);
}
Prachi
  • 3,478
  • 17
  • 34