基于用户输入的javascript数组搜索
嘿,伙计们,我正在尝试搜索一个数组,并根据用户输入显示搜索结果 我的程序的一个演练是读入一系列学生的名字和分数,并确定他们的分数。之后,我想搜索一个学生并显示他的名字、分数和成绩 目前为止,我一直没有找到打印学生基于用户输入的javascript数组搜索,javascript,node.js,Javascript,Node.js,嘿,伙计们,我正在尝试搜索一个数组,并根据用户输入显示搜索结果 我的程序的一个演练是读入一系列学生的名字和分数,并确定他们的分数。之后,我想搜索一个学生并显示他的名字、分数和成绩 目前为止,我一直没有找到打印学生 "use strict" const readline = require('readline-sync'); var name, marks; var studentList = []; input(); search(); function printList(list) {
"use strict"
const readline = require('readline-sync');
var name, marks;
var studentList = [];
input();
search();
function printList(list) {
for (const entry of list) {
// Get a local for `marks` via destructuring
const {marks} = entry;
if (marks > 100 || marks < 0) {
throw new Error(`Invalid 'marks' value: ${marks}`);
} else if (marks >= 80) {
entry.grade = 'HD'
} else if (marks >= 70) {
entry.grade = 'D'
} else if (marks >= 60) {
entry.grade = 'C'
} else if (marks >= 51) {
entry.grade = 'P'
} else { // No `if (marks >= 0)` because we know it is, otherwise we would have thrown an error above
entry.grade = 'N'
}
console.log(entry);
}
function searchStudent(searchName){
for (let i = 0; i<= studentList.length; i++){
if(studentList[i] == searchName){
console.log(studentList[i]);
}
else {
console.log("student not found");
}
}
}
function input() {
while (true)
{
console.log('Please enter the student name (or "end" to end): ');
const name = readline.question('Student Name: ');
if (name === 'end')
{
printList(studentList);
break;
}
console.log('Student Name is' , name);
const marks = readline.question('Enter marks for ' + name + ': ');
if (marks === 'end')
{
printList(studentList);
break;
}
console.log('Marks for ' + name + ' are ' + marks );
studentList.push({name:name, marks: parseFloat(marks)});
}
}
function search() {
while (true)
{
console.log('Please enter the name of student to search for: (or "stop" to end search): ');
const searchName= readline.question("Student Name: ");
if(searchName === 'stop'){
break;
}
searchStudent(searchName);
}
}
“使用严格的”
const readline=require('readline-sync');
变量名称、标记;
var studentList=[];
输入();
搜索();
函数打印列表(列表){
for(列表的常量输入){
//通过分解结构获取“marks”的本地
常量{marks}=条目;
如果(标记>100 | |标记<0){
抛出新错误('Invalid'marks'值:${marks}`);
}否则如果(标记>=80){
entry.grade='HD'
}否则如果(标记>=70){
entry.grade='D'
}否则如果(标记>=60){
entry.grade='C'
}否则如果(标记>=51){
entry.grade='P'
}else{//No`if(marks>=0)`因为我们知道它是,否则我们会在上面抛出一个错误
entry.grade='N'
}
控制台日志(条目);
}
函数searchStudent(searchName){
对于(设i=0;i),您需要修复以下问题:
替换for(让i=0;我尝试替换for(让i=0;istudentList由对象填充)行studentList.push({name:name,marks:parseFloat(marks)})
,但searchName是一个字符串。我想您想将if(studentList[i]==searchName)
更改为if(studentList[i].name==searchName)
没错。学生属于对象StudentList
function searchStudent(searchName) {
for (let i = 0; i < studentList.length; i++) {
if (studentList[i].name == searchName) {
console.log(studentList[i]);
return;
}
}
console.log('student not found');
}
function searchStudent(searchName) {
const student = studentList.find(student => student.name === searchName);
if (!student) {
console.log('student not found');
return;
}
console.log(student);
}