/    Sign up×
Community /Pin to ProfileBookmark

Can’t figure out a loop

I’ve tried every possible loop combination (although i m new to javascripting) and nothing worked. This version has a working loop but stops after user enters nothing and hits OK. Prompt should be asking same question until nothing entered. At that point all the ‘results’ (whatever was entered in previous n prompts) should be retrieved and processed. End result should look like this (if ‘cs a 4’ and ‘bb b 3’ were entered for 2 prompts):

COURSE GRADE HOURS
cs a 4
bb b 3

Total grade points = 28
Number of hours = 7
GPA = 4.000

SCRIPT

[code=html]
<!doctype html>
<html>
<head>
<meta charset=”UTF-8″>
<title>Untitled Document</title>
</head>
<script type=”text/javascript”>

function getData(){
var input=0,
results=[];

while(input!==””){input = prompt(“Enter course name, grade and credit hours (e.g., ‘CS3520 A 4’ or click OK with no data to terminate.”);
input = input.split(” “);
if(input==””){
results.push({
course: input[0].trim(),
grade: input[1].trim(),
creditHours: parseInt(input[2], 10)
});}}
return results;
}

var app = function() {
var result, results,
COLUMN_SEPARATOR = “tt”;

document.writeln(“<pre>”);
results = getData();

document.writeln(“COURSE” + COLUMN_SEPARATOR + “GRADE” + COLUMN_SEPARATOR + “HOURS”);
for (var i = 0, j = results.length; i < j; i++) {
result = results[i];

document.writeln(result.course + COLUMN_SEPARATOR + result.grade + COLUMN_SEPARATOR + result.creditHours);
}

document.writeln();
computeGPA(results);
document.writeln(“</pre>”);
}

window.onload = app;

</script>
<body>
</body>
</html>
[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@jedaisoulJul 05.2015 — Hi and welcome to the site.

Try moving the script to just above </body>?
×

Success!

Help @Cido_SB 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 6.15,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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