How to Change the Value in an Array

Asked 1 years ago, Updated 1 years ago, 84 views

Prerequisites/What you want to achieve

I'm doing a grade processing task with JavaScript.
(The task is to enter a student's grade (score) and display the average and highest scores.)

The content is

First, enter the number of people, and then enter your name and score for that number of times.

Example: 入力 Input screen is performed by prompt.
Input Screen: Please enter the number of people Input: 3
Input screen: Please enter your name Input: Yamada
Input Screen: Enter Score Input: 60
Input Screen: Please enter your name Type: Tanaka
Input Screen: Enter Score Input: 70
Input screen: Please enter your name Input: Suzuki
Input Screen: Please enter your score. Input: 80

Then, you will see the menu entry screen similar to the one below.

1: Correction 2: Average score 3: Highest score 0: Finished

If you enter 1, it says, "Please enter your name."
For example, if you type "Tanaka," you will be asked "How many points is it?" and you will enter your score.
This will change Tanaka's score.

Enter 2 to get an average score.

If you enter 3, you'll get 80 points for Suzuki.

Enter 0 to exit.

終了If it does not end, the menu input screen will be displayed again.

Problems/Error Messages you are experiencing

When you select 1 (correct) on the menu input screen, you can enter the name of the person you want to change.
It's a movement that I'm looking forward to, but what's the score after that didn't work out.
If you enter a value, the score of the person who wants to change will change.

Source Codes Affected


var students = parseInt( prompt('Please enter number of students')); // 2
var studentsArray=[];//[2]
studentsArray.push(students); // Manage the number of people

varnameArray=[]; // Array to manage names
varscoreArray=[]; // Array to manage scores

vartotalScore=0;// Variable to get average points
varmaxScore=0;// Highest score
varmaxScorePerson="";// Highest-scoring student

for(leti=0;i<students;i++){

  var studentName=prompt('Please enter a student's name');
  var studentScore=parseInt('Please enter your student score''));

  nameArray.push(studentName);
  scoreArray.push(studentScore);

  totalScore=totalScore+scoreArray[i]; // Total score

  if(studentScore>maxScore){
    maxScore=studentScore;// Highest score
    maxScorePerson=studentName;// Highest-scoring student
  }

  if(students==studentsArray.length+i){
    varmenu=parseInt(prompt('1:Modified 2:Average 3:Maximum 0:End')/Menu;
    if(menu===1){
      studentName=prompt('Please enter the name of the student you want to change');
      if(studentName==studentName[i]){
        nameArray[i] = studentName;
      }
      if(nameArray[i]!=studentScore[i]){
        scoreArray[i] = parseInt('Please enter the changed score''));
      }
    } else if(menu===2){
      alert("The average score is ""+totalScore/students+""");
    } else if(menu===3){
      alert("The highest score is ""+maxScore+"" and ""+maxScorePerson+""."");
    } else if(menu===0){
      alert('Finish');
    }
  }
}
console.log(nameArray);
console.log(scoreArray);

Tried

 if(menu===1){
      studentName=prompt('Please enter the name of the student you want to change');
      if(studentName==studentName[i]){
        nameArray[i] = studentName;
      }
      if(nameArray[i]!=studentScore[i]){
        scoreArray[i] = parseInt('Please enter the changed score''));
      }

Two students each type [Mr. A, Mr. B] [10 points, 20 points] and
I tried to change Mr. A's score to 100 points with the above code. If you print studentArray and scoreArray on the console,
[Mr. A and Mr. B] [10 points, 100 points];
The score of Mr. B will be changed.
I don't know how to deal with it.
Thank you for your cooperation.

javascript array

2022-09-30 19:21

1 Answers

The current code states that the menu is displayed when studentsArray.length+i is valid, but studentsArray is 1 in length, so i===students-1 always corrects the last student.

Perhaps what you would like to do is close the loop once you have completed the first input process as follows and start a loop to display a new menu.

for(leti=0;i<students;i++){
  // initial input process
}

// Menu Display Loops
while(true){
  varmenu=parseInt(prompt('1:Modified 2:Average 3:Maximum 0:End'));
  if(menu===1){
    studentName=prompt('Please enter the name of the student you want to change');
    // Look for a student index with the name studentName
    for(leti=0;i<students;i++){
      if(studentName==nameArray[i]){
        scoreArray[i] = parseInt('Please enter the changed score''));
      }
    }
  }
  // If menu === 2, 3
  } else if(menu===0){
     alert('Finish');
     break;
  }
}


2022-09-30 19:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.