I've tried everything that crossed my mind, but no luck with this loop (although i'm pretty new to javascript).
Prompt should ask a question until nothing entered. At that point all the 'results' that were previously entered should be taken and processed.
Result should look like (if entered for 1st prompt: 'CS A 4', 2nd 'BB B 3', 3rd 'CC C 3'..):....showing only after there was not entry for nth prompt
COURSE GRADE HOURS
CS A 4
BB B 3
CC C 3
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>gpa.html</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 = "\t\t";
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>
1 Answer 1
Removed (below the split): if(input=="")
Added (above the split): if (input === "") { break; }
This should do it:
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.");
if (input === "") { break; }
input = input.split(" ");
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 = "\t\t";
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();
document.writeln("</pre>");
}
But i think that this would be an even better solution:
function getData() {
var input = true,
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.");
if (!input) { break; }
input = input.split(" ");
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 = "\t\t";
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();
document.writeln("</pre>");
}
Because the return value of canceled prompt() depends on the browser. In most browsers the return value is null. However, some very old browsers (e.g. early versions of IE) used to return '' (an empty string).
So instead of using something like if (input != '' && input != null), just use true or false.
User pressed OK, but the input field was empty input === ""
User typed something and hit OK (or enter) input == true
User pressed CANCEL input == null or input == ""
UPDATE
About the textarea thing, try something like this (i didn't test it):
textareaContentByLines = textareaContent.split("\n");
for(index = 0; index < textareaContentByLines.length; index++){
input = textareaContentByLines.split(" ");
results.push({
course: textareaContentByLines[index][0].trim(),
grade: textareaContentByLines[index][1].trim(),
creditHours: parseInt(textareaContentByLines[index][2], 10)
});
}
2 Comments
textarea for every new line and then you split each line like you did before. I have added an example for the textarea in at the bottom of my answer.
if(input==""){in yourgetDatafunction be something along the lines ofif(input.length){? I don't think your current statement will ever be true.