/    Sign up×
Community /Pin to ProfileBookmark

why does "cancel" cancel the whole script ?

<script type=”text/javascript” language=”JavaScript”>

var i = 0;

function Questions(q) {
var ans=prompt(q,””);
return ans.toUpperCase();
}

q1=”What color is the sky?nA) Red ? Pink C) Black D) Blue”;
r1 = Questions(q1);
if(r1 == “D”) { alert(“Correct!”); i++} else { alert(“Wrong…”); };

q2=”What color is the grass?nA) Red ? Pink C) Green D) Blue”;
r2 = Questions(q2);
if(r2 == “C”) { alert(“Correct!”); i++} else { alert(“Wrong…”); };

alert(“You got ” + i + ” correct!”);

</script>

…..var ans = “…….why, when the user clicks on “cancel”, does the above whole script get cancelled, and is there a way i can get to continue ??”

if(ans || !ans)
{
document.write(“Thanks very much.”)
}

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@FangNov 23.2006 — 'cancel' returns [I]null[/I], so ans.toUpperCase(); throws an error.

Use:return (ans)? ans.toUpperCase() : ans; // ternary operator, same as [I]if.. else..[/I]
Are you learning from these homework assignments?
Copy linkTweet thisAlerts:
@changintimesauthorNov 24.2006 — are you kidding ? i'm learning all the time, i'm very new at javascript,

going to try your suggestion now,
Copy linkTweet thisAlerts:
@changintimesauthorNov 24.2006 — i think i know why you asked me that now,

i'm learning all over the place, i have my own style, i will be a great javascript programmer someday,

.........your suggestion worked fine,
×

Success!

Help @changintimes 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.17,
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,
)...