0

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>
Yasel
3,1784 gold badges43 silver badges49 bronze badges
asked Jul 4, 2015 at 22:58
2
  • Where exaclty is that you are getting this loop? Commented Jul 4, 2015 at 23:10
  • shouldn't if(input==""){ in your getData function be something along the lines of if(input.length){? I don't think your current statement will ever be true. Commented Jul 5, 2015 at 1:28

1 Answer 1

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)
 });
}
answered Jul 5, 2015 at 1:59
Sign up to request clarification or add additional context in comments.

2 Comments

@peter Hmm, another thing if i may ask :-). Similar scenario as above, if i have a text area instead of prompt and each course, grade and hours are separated by a new line, how can i read and create subarrays course, grade, hours from each line and use it later on for other things.
First you split the value of the 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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.