151k views
1 vote
Returning an array element in an array of strings The string array classRoster is a row array of string scalars with the student names. The array entry corresponds to the seat number of the student. Write a function that returns the name of the student in the seat given the string array class Roster and the seat number. Ex: class Roster=["Jill Applegate"; "John Monroe"; "Cristian Phillips"); studentName = StudentLookup(classRoster, 2) returns studentName = "John Monroe" Then, call this function to run the case below. StudentLookup(["Megan"; "Greg"; "Jacob"; "Tony"), 3)

1 Answer

1 vote

Final answer:

To write a function that returns the name of the student in a given seat number from an array of strings, you can use the seat number as the index to access the corresponding element in the array.

Step-by-step explanation:

To write a function that returns the name of the student in a given seat number from an array of strings, you can use the seat number as the index to access the corresponding element in the array. Here's an example:



function StudentLookup(classRoster, seatNumber) {
return classRoster[seatNumber - 1];
}



In this example, the seat number is subtracted by 1 since arrays are zero-based, meaning the first element is at index 0. The function would return the name of the student in the specified seat.



To call this function with the provided class Roster and a seat number, you can use the following code:



var classRoster = ["Jill Applegate", "John Monroe", "Cristian Phillips"];
var studentName = StudentLookup(classRoster, 2);
console.log(studentName);



This would output "John Monroe" to the console. You can replace the values in the classRoster array and the seatNumber argument to lookup different students.

User Baltekg
by
9.0k points