How do you declare variables x1, x2,...,x10 using a loop function in JavaScript?

时间:2016-02-12 20:28:32

标签: javascript loops for-loop logic javascript-objects

I am trying to run the following code in JavaScript to accomplish the following task: declare a series of variables that essentially have the same name but are only different in their suffix, which is an integer, e.g. x1,x2,x3... and so on.

I am new to JavaScript (in fact, I'm entirely new to the coding world). The following code is what I thought made logical sense but it doesn't want to execute the way I want it to.

TEST(TestCase1, Test1){
    .....
}
TEST(TestCase1, Test2){
    ....
}

There's something else I wish to do...

Say, for example, I made a construct such as the following and then proceeded to create customised objects (student1, student2, student3). My end goal is to print out, sequentially, the student's name and a comment on their grade. Instead of having to type out the function that would print this out for each student I would like to write a loop that would make this process more efficient. However, trying to do this has proven a bit difficult. Here is the code:

for (i = 1; i<11; i++) {
  var x+i = 'variable'+i;
  }

Can someone tell me what logic of JavaScript I am not fully understanding that does not allow the above to execute, and is there another way?

4 个答案:

答案 0 :(得分:4)

As jfriend00 says, the piece of javascript that you're looking for is arrays.

Whenever you find yourself adding numbers to variable names, that should indicate that you could put them in an array.

So you could replace this:

import random
print("Welcome to RNG Guesser!\n")
gld = random.randrange(1,10)
counter = 0
ccounter = 0

while True:
    print("Number of tries: {}".format(counter))
    print("Number of correct guesses: {}".format(ccounter))

num = input("Enter a number: ")

if num is "exit":
    print("Number of tries: {}".format(counter))
    print("Number of correct guesses: {}".format(ccounter))
    break
else:

    if int(num) is gld:
        print("Congratulations, your guessed number {} was right!".format(num))
        counter += 1
        ccounter += 1

    elif int(num) < gld:
        print("Pick a higher number!")
        counter += 1

    else:
        print("Pick a lower number!")
        counter += 1

with this:

var student1 = new Student("Candice R.", "A");
var student2 = new Student("Robert K.", "C");
var student3 = new Student("Steven M.", "F");

Now all three of those students are stored together in the var students = []; students.push(new Student("Candice R.", "A")); students.push(new Student("Robert K.", "C")); students.push(new Student("Steven M.", "F")); array. The first student is accessed at students, and your for loop should be able to iterate over them easily.

There are perhaps better ways to define this array of students, but this is a pretty close translation of your existing code.

答案 1 :(得分:1)

While you could use an array to store your variables and use numeric indices to access them like this:

<input type="text" class="form-control" value="13-02-2016" name="expectedDlivery">

if you really wanted to use the names "student1", "student2", "student3", you could do something like:

var students = []; // create an array and add student objects to it
students[0] = new Student("Candice R.", "A");
students[1] = new Student("Robert K.", "C");
students[2] = new Student("Steven M.", "F");

for (i = 0; i < students.length; i++) {
  students[i].print();
}

To answer the question of creating such variables in a loop:

var students = {}; // create an empty object and add students as properties
students["student1"] = new Student("Candice R.", "A");
students["student2"] = new Student("Robert K.", "C");
students["student3"] = new Student("Steven M.", "F");

for (i = 1; i < 4; i++) {
  students["student" + i].print();
}

答案 2 :(得分:1)

Another name for variable is "identifier." Every programming language one can think of restricts names to a specific combination of characters. For: var x+i;

The interpreter sees this and the declaration is an invalid set of characters for an identifier in JavaScript...because it has + operator. Common restriction is not starting a variable name with a number. Restricting to $/_/[A-Z] for the first character and then a mix of numbers...

What you are looking for is to use a data structure such as an array or a hash table.

- languages
 -- copy-this
  --- copy-this.txt
 -- af
  --- copy-this.txt
 -- am
  --- copy-this.txt

答案 3 :(得分:-1)

Use this and it should be ok:

ClassName(ClassName&& other)
   : _data(std::forward(other._data))
{
}