Samuel Tang
Samuel Tang

Reputation: 71

Javascript adding to array using readline

I'm trying to repeatedly get 2 inputs and store them into an array until the word 'end' is typed. However, I'm getting undefined at the console.log(studentList[i]);

EDIT: With the help of you guys, I was able to store the values into the array. Right now what I want to do is to give a 'grade' to each of the marks that was entered. However no matter what number i entered, i was getting 'HD' for all the grades.

const readline = require('readline-sync');

var name, marks;
var studentList = [];

Input();

function printList(list) {
  for (let i = 0; i < studentList.length; i += 1) {
    var grade;
      if ((marks <= 100) && (marks => 80)){
          grade = 'HD'
          studentList[i][2] = grade;  

        } 
        else if ((marks <= 79) && (marks => 70)) 
        {
            grade = 'D'
            studentList[i][2] = grade;

        } 
        else if ((marks <= 69) && (marks =>60)) 
        {
            grade = 'C'
            studentList[i][2] = grade;

        } 
        else if ((marks <= 59) && (marks =>51)) 
        {
            grade = 'P'
            studentList[i][2] = grade;

        } 
        else if ((marks < 50) && (marks =>0)) 
        {
            grade = 'N'
            studentList[i][2] = grade;

        }
        console.log(studentList[i]);
    }       

}

function Input()
{
  while(true) {
    console.log("Please enter the student name (or \"end\" to end): ");
    name = readline.question("Student Name: ");
    if (name === 'end') {
      printList(studentList)
      break
    }
    console.log("Student Name is" , name);
    marks = readline.question("Enter marks for " + name + ": ");
    if (marks === 'end') {
      printList(studentList)
      break
    }
    console.log("Marks for " + name + " is " + marks );
    studentList.push([name, marks]);
  }
}

Any advice would be much appreciated! Thanks!

Upvotes: 0

Views: 922

Answers (2)

Ben Aston
Ben Aston

Reputation: 55759

I don't think you know how many students there are ahead of time, so you will need to loop until "end" is detected.

This might help:

const readline = require('readline-sync');

var name, marks;
var studentList = [];

Input();

function Input() {
    while (true) {
        console.log("Please enter the student name (or \"end\" to end): ");
        name = readline.question("Student Name: ");
        if (name === 'end') break;
        console.log("Student Name is", name);
        marks = readline.question("Enter marks for " + name + ": ");
        console.log("Marks for " + name + " is " + marks);
        studentList.push([name, marks]);
    }
}

for (var i = 0; i <= studentList.Length; i++) {
    console.log(studentList[i]);
}

Upvotes: 1

Ricardo Pierre-Louis
Ricardo Pierre-Louis

Reputation: 184

You mainly need to change .Length to .length and you have to use a while loop combined with a break (once 'end' is typed) that comes out of the while loop to give you the list you want to print. By changing the location of the if statements you are able to adjust when the break can occur and when it reads from users input.

const readline = require('readline-sync');

var name, marks;
var studentList = [];

Input();

function printList(list) {
  for (let i = 0; i < list.length; i += 1) {
    console.log('list[i]', list[i]);
  }
}

function Input()
{
  while(true) {
    console.log("Please enter the student name (or \"end\" to end): ");
    name = readline.question("Student Name: ");
    if (name === 'end') {
      printList(studentList)
      break
    }
    console.log("Student Name is" , name);
    marks = readline.question("Enter marks for " + name + ": ");
    if (marks === 'end') {
      printList(studentList)
      break
    }
    console.log("Marks for " + name + " is " + marks );
    studentList.push([name, marks]);
  }
}

Upvotes: 1

Related Questions