/    Sign up×
Community /Pin to ProfileBookmark

need help with nesting

I’m clearly doing this wrong. I’m trying to nest a while loop in a function under the assumption that variables are global. Clearly they are not. How can I do this correcty?

[code]
<SCRIPT>
var xpos=0;
var ypos=0;
var rowisoffest=0;

function drawrow(){
while(xpos<576){
document.write(‘<img style=”position:absolute;left:’+ xpos +’px;top:’+ ypos +’px;” src=”floor.gif”>’);
var xpos=xpos+64;
}
}
while(ypos<320){
if(rowisoffset==1){
var xpos=32;
var rowisoffest=0;
}else{
var xpos=0;
var rowisoffset=1;
}
drawrow();
var ypos=ypos+16;
}

</SCRIPT>
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMar 23.2009 — One set of variables (xpos and ypos) is global

but another set of xpos and ypos inside the function are local.

Depending upon what you are trying to do either

1. rename one set of the xpos, ypos variables

or

2. remove the "var" inside the function that creates another set of xpos,ypos local variables.
Copy linkTweet thisAlerts:
@trandrusMar 23.2009 — [CODE]
<SCRIPT>

var app = {
xpos:0,
ypos:0,
rowisoffset:0,

drawRow: function() {
while(this.xpos<576)
{
document.write('<img style="position:absolute;left:'+ this.xpos +'px;top:'+ this.ypos +'px;" src="floor.gif">');
this.xpos=this.xpos+64;
}
}
};

while(app.ypos<320){
if(app.rowisoffset===1){
app.xpos=32;
app.rowisoffest=0;
}else{
app.xpos=0;
app.rowisoffset=1;
}
app.drawrow();
app.ypos=app.ypos+16;
}

</SCRIPT>
[/CODE]


as you can see, there are some general scoping problems with your logic. you should try to contain everything, all properties and methods, in one global application controller object. otherwise, scoping will quickly become an untamable beast
Copy linkTweet thisAlerts:
@analoganomalyauthorMar 23.2009 — <duplicate post deleted by user>
Copy linkTweet thisAlerts:
@analoganomalyauthorMar 23.2009 — ahh okay, so functions hold their own local variables. Thanks! Works great now. One more question, if you wouldn't mind. Can i have two seperate functions with the same local variable names without mussing things up?

Also, Trandrus, i missed your reply, but appreciate the valuable information. I've been attempting to refresh my knowledge of js with a project i don't really expect to do much with. I do appreciate the information and will take it into account in the future. =) though, testing the code you provided i error out with app.drawrow is not a function. =x

For anyone curious (and while I know this is probably not the most efficent or sound way of acheiving this, it works);

<i>
</i>&lt;HTML&gt;
&lt;head&gt;
&lt;SCRIPT&gt;
function drawfloors(){
var xpos=155;
var ypos=-11;
var rowisoffset=0;

while(ypos&lt;256){
if(rowisoffset==1){
var xpos=187;
while(xpos&lt;608){
document.write('&lt;img style="position:absolute;left:'+ xpos +'px;top:'+ ypos +'px;" src="floor.gif"&gt;');
var xpos=xpos+64;
}
var rowisoffset=0;
}else if(rowisoffset==0){
var xpos=155;
while(xpos&lt;608){
document.write('&lt;img style="position:absolute;left:'+ xpos +'px;top:'+ ypos +'px;" src="floor.gif"&gt;');
var xpos=xpos+64;
}
var rowisoffset=1;

<i> </i>}
var ypos=ypos+16;
}

}

drawfloors();
&lt;/SCRIPT&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div style="position:absolute;top:0;left:0;"&gt;&lt;img src="skin.gif"&gt;&lt;/div&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
Copy linkTweet thisAlerts:
@trandrusMar 24.2009 — Can i have two seperate functions with the same local variable names without mussing things up?[/QUOTE]
yes. their names will only apply within the scope of the function in which they are contained. whether or not it "musses things up" is debatable as it's less manageable than a class- or object-based approach.

testing the code you provided i error out with app.drawrow is not a function[/QUOTE]
sorry, case-sensitive: app.drawRow();
Copy linkTweet thisAlerts:
@analoganomalyauthorMar 24.2009 — Thanks again, really appreciated. =)
×

Success!

Help @analoganomaly 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.6,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...