/    Sign up×
Community /Pin to ProfileBookmark

For loop with complex variable

I need to see output as cat,dog,fish,bird what syntax I suppose to use.

<script>
a1=cat;
a2=dog;
a3=fish;
a4=bird;
for (var i = 1;i <= 4; i++){
document.writeln(ai);
}
</script>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@PadonakJul 02.2013 — <i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;123&lt;/title&gt;
&lt;style type="text/css"&gt;
body{color:#000;background-color:#fff;font-family:'Bookman Old Style',Georgia,Verdana;font-size:14px;text-align:center;font-weight:bold;margin-top:300px;}
&lt;/style&gt;
&lt;script type="text/javascript"&gt;
var animals=['cat','dog','fish','bird'];
window.onload=function(){document.getElementById('output').innerHTML=animals;}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="output"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@Ay__351_eJul 02.2013 —  <br/>
&lt;script type="text/javascript"&gt;

// http://www.w3schools.com/jsref/jsref_obj_string.asp
// http://www.w3schools.com/jsref/jsref_slice_string.asp
// http://www.w3schools.com/jsref/jsref_eval.asp

var a1 = 'cat';
var a2 = 'dog';
var a3 = 'fish';
var a4 = 'bird';
var str = '';

for (var i = 1;i &lt;= 4; i++){

str = str + eval('a'+i) + ',';

}

alert(str); // cat,dog,fish,bird,

str = str.slice(0, str.length-1);

alert(str); // cat,dog,fish,bird

document.write(str);

&lt;/script&gt;
×

Success!

Help @kkakarot spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...