-5

In JavaScript, how do I make a for loop that would make 7 variables with the same value,but different names. So, I want to take a string and subtract the last two letters. I do this with

var stringExample = prompt("Blah blah"); 
var stem = stringExample.substring(0, stringExample.length-2);

And then make 6 more of the stem variables with the names of stem0 through stem6. Right now my code is:

for (var i = 0; i < 7; i++) {
    eval('var stem' + i + '= toDecline.substring(0, toDecline.length - 2');
};
  • 4
    So you want an array... – elclanrs Sep 04 '14 at 18:38
  • 2
    Most likely there is a better way to do what ever you are trying to do. Post what you are trying to do. – Matt R Sep 04 '14 at 18:42
  • possible duplicate of [Use dynamic variable names in JavaScript](http://stackoverflow.com/questions/5117127/use-dynamic-variable-names-in-javascript) – Teemu Sep 04 '14 at 19:02

3 Answers3

1
var stem = stringExample.substring(0, stringExample.length-2);
var stem0 = stem1 = stem2 = stem3 = stem4 = stem5 = stem6 = stem;

Note some implications with regard to scope when doing this. Essentially, the subsequent variables are initialized in the global namespace. Remedy that by defining them in advance.

That said, I suspect that your application logic could be simplified to avoid needing 7 identical variables.

Community
  • 1
  • 1
isherwood
  • 58,414
  • 16
  • 114
  • 157
  • how about: `var stem0 = stem1 = stem2 = stem3 = stem4 = stem5 = stem6 = stringExample.substring(0, stringExample.length-2);` – Ismael Miguel Sep 04 '14 at 19:03
  • @isherwood I keep trying to figure out how to solve the problem of the 7 variables. Later, I add text to the end of the string with using `var nom = stem0 += 'a';`. But then, when I add another line `var gen = stem0 += 'ae';` it ends up making the vale of `gen` the value of `nom += 'ae'` I need seven variables because I make a var like nom or gen 7 times, and it just keeps tacking more text on the end. Any ideas on that? – Will Hodges Sep 04 '14 at 19:12
  • using `var stem0 = stem1 = stem2 = stem3 = stem4 = stem5 = stem6 = stem;` worked. – Will Hodges Sep 04 '14 at 19:29
  • @n00bie If you aren't declaring those other variables earlier, then they will be global, which is generally a REALLY bad idea. – forgivenson Sep 04 '14 at 19:33
1

you can have it like this:

  var stem = stringExample.substring(0, stringExample.length-2);
    var stemr=[];
    for (var i = 0; i < 7; i++) {
    stemr[i]=stem;
    }
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
1

Just use an array.

var stemArray = [];
var value = stringExample.substring(0, stringExample.length-2);

for (var i = 0; i < 7; i++) {
    stemArray[i] = value;
};
forgivenson
  • 4,394
  • 2
  • 19
  • 28
  • Hmmm....Maybe my code is off, but that did not work. `var toDecline = prompt("Enter the word's genitive"); var stem = toDecline.substring(0,toDecline.length - 2); for (var i = 0; i < 7; i++) { all1[0] = stem += 'a'; all1[1] = stem += 'ae'; all1[2] = stem += 'ae'; all1[3] = stem += 'am'; all1[4] = stem += 'a'; all1[5] = stem += 'ae'; all1[6] = stem += 'arum'; all1[7] = stem += 'is'; all1[8] = stem += 'as'; all1[9] = stem += 'is'; }; console.log(all1);` P.S. I am making a Latin word decliner. – Will Hodges Sep 04 '14 at 19:22
  • What doesn't work? Btw, in your code, that loop does nothing. – forgivenson Sep 04 '14 at 19:32