0

I am fairly new to Javascript and have been picking it up pretty quickly in the past few days, after staring at my code for hours and trying to figure out why it wasn't working the way I intended i figured i would post it here.

Anyways, so my question is how do I display the WHOLE content of an array after comma splitting it. My code is below. My code is only printing out the last number set that I input at the prompt.

Help would be highly appreciated.

var gradeinfo = new Object(); {

    coursecode = new Array;
    grade = new Array;

};

var changer = function (y) {

    finalgradeinfo = new Array;
    finalgradeinfo = y;

    function newresults() {

        var i = 0;
        finalgradeinfo[i] = y;
        i + 1;

    }
    return finalgradeinfo;
}

do {

    var entry = prompt("Enter a course code and grade seperated by a comma");

    if (entry != null && entry != "") {

        var counter;
        var entryvalid = new Array;
        entryvalid = entry.split(",");
        changer(entryvalid);

        x = true;

    } else {

        x = false;
    }

} while (x != false);

console.log(finalgradeinfo);

My function needs to include closure so if it looks entirely wrong i apologize in advance.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
mike
  • 3
  • 3

3 Answers3

0

Help from this post

Split creates an array already. So, if you enter 1,2,3, you get an array like this when you split it: ["1", "2", "3"]. In your for loop, you are getting the characters from the original input, not your array. In order to add them, you need to change the input to numbers since they are considered strings. So your for loop should look like this:

for (i=0; i<3; i++)
{
    entryArray[i] = parseFloat(entryArray[i]);
}

overwriting the strings with the digits.

Community
  • 1
  • 1
Amol
  • 1,431
  • 2
  • 18
  • 32
0

In changer() you're destroying and recreating the array after each input. I suggest moving the array declaration into the global scope so that you can just push elements to it in the changer() function:

Fiddle

var finalgradeinfo = [];

var changer = function (y) {
    finalgradeinfo.push(y);
}

do {
    var entry = prompt("Enter a course code and grade seperated by a comma");

    if (entry != null && entry != "") {
        var counter;
        var entryvalid = entry.split(",");
        changer(entryvalid);
        x = true;
    } else {
        x = false;
    }
} while (x != false);

console.log(finalgradeinfo);

Notes:

  • Declaring arrays as [] is preferred to new Array
  • Not sure if you're aware but the newresults() function and gradeinfo object aren't doing anything
Community
  • 1
  • 1
MrCode
  • 63,975
  • 10
  • 90
  • 112
0

Also, the counter doesn't do anything, and the x boolean is unnecessary because it's basically just checking for prompt input. Here is my approach and fiddle.

var finalgradeinfo = { // declare finalgradeinfo in the global scope
    coursecode: [],
    grade: [] }
  , entry = '';

do {
    entry = prompt('Enter a course code and grade seperated by a comma') || ''; // will set entry to '' if user hits cancel
    if (entry == '') continue; // break out of iteration if string is empty
    var entryvalid = entry.split(",");
    finalgradeinfo.coursecode.push(entryvalid[0]);
    finalgradeinfo.grade.push(entryvalid[1]);
} while(entry !== '');

console.log(finalgradeinfo);
Robert Falkén
  • 2,287
  • 2
  • 16
  • 16