/    Sign up×
Community /Pin to ProfileBookmark

Beginner struggling with do/while statement, help!

Hello all!

I’m pretty new to this and I need someone to help me understand what I’ve done wrong. I’ve created code on a web page that contains a ‘while statement’ and a ‘do/while statement’. When I click the ‘try button’ on the webpage only one will work. The while and do/while statement won’t work in the same document, but if I put the codes in separate documents they will work. I’d be grateful if someone could help as I’ve no idea what to do. I’ve pasted the code below.

Best wishes

Bob.

<!DOCTYPE html>
<html>
<body>

<p>Click the button to loop through a block of code as long as i is less than 5.</p>

<button onclick=”myFunction()”>Try it</button>

<p id=”demo”></p>

<script>
function myFunction() {
var text = “”;
var i = 0;
while (i < 5) {
text += “<br>The number is ” + i;
i++;
}
document.getElementById(“demo”).innerHTML = text;
}
</script>

</body>

<body>

<p>Click the button to loop through a block of code as long as i is less than 5.</p>

<button onclick=”myFunction()”>Try it</button>

<p id=”demo”></p>

<script>
function myFunction() {
var text = “”
var i = 0;
do {
text += “<br>The number is ” + i;
i++;
}
while (i < 5);
document.getElementById(“demo”).innerHTML = text;
}
</script>

</body>
</html>

to post a comment
CSSFull-stack Developer

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERFeb 18.2019 — Couple of problems you can correct.

1. You cannot have two body statements within the same script. (Might work if you are a Siamese twin ? )

2. You have two functions with the same name. When they are in the same program which one do you think will execute? The first or the last?
×

Success!

Help @bobbed12 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.19,
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,
)...