0

I try to make code like this:

var code1 = a, code2 = b, code3 = c;
var x = 3;

for (y = 1; y <= x; y++) {
  //this part where i dont know about
  alert ();
}

So how to make it alert code1, code2, and code3? I mean this alerts the values a, b, and c. I tried with alert("code"+y); and alert(code+y); but it wont do.

0

2 Answers 2

9

So how to make it alert code1, code2, and code3? i mean this alert the value a, b, and c?

The best way is to use an array instead of discrete code1, code2, and code3 variables:

// (I assume a, b, and c have already been declared somewhere, or that your real
// code has literals?)
var codes = [a, b, c];
var y;

for (y = 0; y < codes.length; y++) {
  alert(codes[y]);
}

(Note that I started y in a different place.)

While it's possible to do the code1, code2, code3 thing with global variables, global variables should be avoided whenever possible, and it's nearly always possible. (It's also possible with local variables, but you have to use eval or its cousin the Function constructor, and avoiding eval is also something you should avoid whenever possible, and is nearly always possible. :-) )

Alternately, if you find yourself wanting to do this where an array doesn't quite make sense, you can use an object instead:

var codes = {
    code1: a,
    code2: b,
    code3: c
};
var y;
for (y = 1; y <= 3; ++y) {
    alert(codes["code" + y]);
}

That works because in JavaScript, you can access an object property using either dot notation and a literal (obj.foo), or brackets notation and a string (obj["foo"]), and in the latter case the string can be the result of any expression. Since "code" + y is code1 when y is 1, codes["code" + y] looks up the property "code1" on codes (when y is 1).

Sign up to request clarification or add additional context in comments.

9 Comments

I actually think this is a "better" solution than the others but obviously not what fixes OP issue...not sure why you guys are killing him with downvotes though
you might be right from educational point of view, but that is not an answer to this question
I do not get it. Is there a downvote-outbreak? This is too a valid answer.
How does this not answer the question? The question is how to alert the values a, b, and c: "i mean this alert the value a, b, and c" This tells the OP the correct way to do that, not a way that only works for global variables, does unnecessary string concatenation, and encourages bad habits.
Agree with you T.J completely. People must of woke up on wrong side of the bed
|
5

Use Bracket notation

 alert(window["code"+y]);

I would rather recommend you to use an array like

var code = [1, 2, 3];
for (y = 0; y < code.length; y++) {
    alert(code[y]);
}

3 Comments

This will only work in the global scope. If you're in any function scope, this will not work. While the answer is correct in theory, it would be much better to advise OP to use an object manually, instead of relying on window.
Scope and also just plainly teaching bad ways in javascript to a newbie could be a reason
@NiettheDarkAbsol The question was: »So how to make it alert code1, code2, and code3?« And this code does, for what the TO asked. I find it okay, to criticize it with a comment. But as long this solution is valid, I see no reason to downvote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.