I am a beginner student. Thanks for your cooperation.

I am solving a program task that returns the course names in an array from the entered course code.
I was able to solve the process of getting the array number from the input value.
I didn't know how to keep it in another array (the array for output) and it got stuck ...

I want to array arr, but I get an error when I do var arr [i] ...

I would appreciate it if you could borrow your wisdom. Thanks for your time.

Source code
// Declaration of array
var courseCode = ["WEB222", "WEB322", "IPC244", "JAV745", "OOP244"];
var courseTitle = ["Web Programming Basics", "Web Programming Tools and Frameworks", "Introduction to C", "Java Programming", "Introduction to Object-Oriented Programming"];
// User input
var noOfCourse = prompt ("Enter the number of courses you took this semester:");
console.log ("\ n");
    // Repeat for the number of user inputs
    for (i = 0;i<noOfCourse;i ++) {
        var inputCode = prompt ("Enter course code:");
            if (courseCode.includes (inputCode)) {// If the array has input values
                var arr = (courseCode.indexOf (inputCode));// Get array number
                //<-Would you write a process to hold an array for user input here? ??
                var mark = prompt ("\ t Please enter your grade:");
            else {// if the array has no input values
                console.log (ʻError: There is no course code ${inputCode} `);
                noOfCourse ++;// Reset the number of repeats
        console.log ("\ n");
// Output
console.log ("Grades are as follows:");
    // Repeat for the number of user inputs
    for (j = 0;j<noOfCourse;j ++) {
        console.log ("----------");
        console.log (`course code: ${courseCode [arr]}`);
        console.log (`course name: ${courseTitle [arr]}`);
        console.log (`Grade: ${mark [j]}`);
Output screen

↑ Since arr is not arrayed, the second input value is simply output ...

  • Answer # 1

    I was able to solve the process of getting the sequence number from the input value.

    I think that the input value is assumed to be the result of prompt (), but the processing assumed in the following four cases isCan notmust.

    The cancel button was clicked

    A decimal value was entered

    A non-numeric character string was entered

    Integer value but not greater than 0

    It is necessary to imagine "various input of the user" and make an appropriate isolation.

    var inputNum = void 0;
    while (! inputNum) {
      inputNum = prompt ("Enter an integer greater than or equal to 1");// You may specify default = 1.
      console.log ("Prompt () result:", inputNum, typeof inputNum, parseInt (inputNum));
      if (! isFinite (inputNum) || (inputNum = parseInt (inputNum))<1) {
        inputNum = undefined;
    console.log ("input integer:", inputNum);

    Prepare a user function to process properly,Get the intended input valuePlease think like this.

    Example of input processing to obtain an integer value of 1 or more

    var inputInteger = function (message) {
      var rslt = undefined;// void 0 in old processing system
      while (! rslt) {
        rslt = prompt (message);
        console.log ("prompt ()>", rslt, typeof rslt, parseInt (rslt));// Erase later
        if (! isFinite (rslt) || (rslt = parseInt (rslt))<1) {
          rslt = undefined;
      return rslt;
    console.log ("Enter integer:", inputInteger ("Enter an integer greater than or equal to 1"));


    prompt ()

    isFinite ()

    parseInt ()

    I want to array arr

    In the code// Get the sequence numberIs commented, but the variable namearrnot,idxTry changing it to a variable name with the prefix (which causes confusion).

    //<-Would you write a process to hold an array for user input here? ??

    i == 0At that time, it seems that the array that stores the result will be initialized.
    (If you don't specify a 2D array, it's easier to use an object instead of an associative array).

  • Answer # 2


    If you just want an array

    let indexes = [];

    After initializing as an array

    indexes.push (1);// Add element "1" to the end

    All you have to do is add elements.

    What I was interested in

    Since var is outdated, use const and let unless you have a specific reason to do so.

    Putting it in a different array as courseCode and courseTitle is not good in terms of design because the code and course name may be out of alignment.
    It is better to do the following using JSON.

    const courses = [
        {"code": "WEB222", "name": "Basics of Web Programming"},
        {"code": "WEB322", "name": "Web programming tools and frameworks"},
    // get the name of index 2
    const name = courses [2] .name;
    // Get the index of the course whose code is WEB222
    const index = courses.findIndex (course =>course.code == "WEB222");