Knowee
Questions
Features
Study Tools

Create a function getStudentsByLocation that returns an array of objects who are located in a specific city.It should accept a list of students (from getListStudents) and a city (string) as parameters.You must use the filter function on the array.bob@dylan:~$ cat 2-main.jsimport getListStudents from "./0-get_list_students.js";import getStudentsByLocation from "./2-get_students_by_loc.js";const students = getListStudents();console.log(getStudentsByLocation(students, 'San Francisco'));bob@dylan:~$ bob@dylan:~$ npm run dev 2-main.js [ { id: 1, firstName: 'Guillaume', location: 'San Francisco' }, { id: 5, firstName: 'Serena', location: 'San Francisco' }]bob@dylan:~$

Question

Create a function getStudentsByLocation that returns an array of objects who are located in a specific city.It should accept a list of students (from getListStudents) and a city (string) as parameters.You must use the filter function on the array.bob@dylan:~cat2main.jsimportgetListStudentsfrom"./0getliststudents.js";importgetStudentsByLocationfrom"./2getstudentsbyloc.js";conststudents=getListStudents();console.log(getStudentsByLocation(students,SanFrancisco));bob@dylan:  cat 2-main.jsimport getListStudents from "./0-get_list_students.js";import getStudentsByLocation from "./2-get_students_by_loc.js";const students = getListStudents();console.log(getStudentsByLocation(students, 'San Francisco'));bob@dylan:~ bob@dylan:~npmrundev2main.js[id:1,firstName:Guillaume,location:SanFrancisco,id:5,firstName:Serena,location:SanFrancisco]bob@dylan:  npm run dev 2-main.js [ { id: 1, firstName: 'Guillaume', location: 'San Francisco' }, { id: 5, firstName: 'Serena', location: 'San Francisco' }]bob@dylan:~

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step guide on how to create the function getStudentsByLocation:

  1. First, you need to import the getListStudents function from the 0-get_list_students.js file. This function should return an array of student objects.
import getListStudents from "./0-get_list_students.js";
  1. Next, define the getStudentsByLocation function. This function should accept two parameters: a list of students and a city.
function getStudentsByLocation(students, city) {
  // function body will go here
}
  1. Inside the function, use the filter method on the students array to return a new array that includes only the students located in the specified city. The filter method takes a callback function that tests each item in the array. In this case, the callback function should check if the location property of each student object matches the city parameter.
function getStudentsByLocation(students, city) {
  return students.filter(student => student.location === city);
}
  1. Finally, you can test your function by calling it with the list of students returned by getListStudents and a city. For example, to get all students located in 'San Francisco', you would do:
const students = getListStudents();
console.log(getStudentsByLocation(students, 'San Francisco'));

This should log an array of student objects located in 'San Francisco' to the console.

This problem has been solved

Similar Questions

Create a function named getListStudents that returns an array of objects.Each object should have three attributes: id (Number), firstName (String), and location (String).The array contains the following students in order:Guillaume, id: 1, in San FranciscoJames, id: 2, in ColumbiaSerena, id: 5, in San Franciscobob@dylan:~$ cat 0-main.jsimport getListStudents from "./0-get_list_students.js";console.log(getListStudents());bob@dylan:~$ bob@dylan:~$ npm run dev 0-main.js [ { id: 1, firstName: 'Guillaume', location: 'San Francisco' }, { id: 2, firstName: 'James', location: 'Columbia' }, { id: 5, firstName: 'Serena', location: 'San Francisco' }]bob@dylan:~$

Create a function getListStudentIds that returns an array of ids from a list of object.This function is taking one argument which is an array of objects - and this array is the same format as getListStudents from the previous task.If the argument is not an array, the function is returning an empty array.You must use the map function on the array.bob@dylan:~$ cat 1-main.jsimport getListStudentIds from "./1-get_list_student_ids.js";import getListStudents from "./0-get_list_students.js";console.log(getListStudentIds("hello"));console.log(getListStudentIds(getListStudents()));bob@dylan:~$ bob@dylan:~$ npm run dev 1-main.js [][ 1, 2, 5 ]bob@dylan:~$

1. Let's make some classroomsmandatoryImport the ClassRoom class from 0-classroom.js.Implement a function named initializeRooms. It should return an array of 3 ClassRoom objects with the sizes 19, 20, and 34 (in this order).bob@dylan:~$ cat 1-main.jsimport initializeRooms from './1-make_classrooms.js';console.log(initializeRooms());bob@dylan:~$ bob@dylan:~$ npm run dev 1-main.js [ ClassRoom { _maxStudentsSize: 19 }, ClassRoom { _maxStudentsSize: 20 }, ClassRoom { _maxStudentsSize: 34 }]bob@dylan:~$

Sample Input 1:Enter the no of student details to be created:3Name: RamAge: 12Location: ChennaiName: RajAge: 14Location: BangaloreName: RaviAge: 17Location: ChennaiSample Output 1:Here's the list of student details:{'Name': 'Ram', 'Age': 12,'Location':'Chennai'}{'Name': 'Raj', 'Age': 14,'Location':'Bangalore'}{'Name': 'Ravi', 'Age': 17,'Location':'Chennai'}Enter the training location: ChennaiStudent(s) enrolled in this training location:RamRaviSample Input 2:Enter the no of student details to be created:2Name: RamAge: 12Location: ChennaiName: VishnuAge: 20Location: MumbaiSample Output 2:Here's the list of student details:{'Name': 'Ram', 'Age': 12, 'Location': 'Chennai'}{'Name': 'Vishnu', 'Age': 20, 'Location': 'Mumbai'}Enter the training location: BangaloreInvalid location

You want to create a list containing the names of three cities: London, Paris, and Tokyo. Which code snippet correctly constructs this list?1.0 Markscities = ["London", "Paris", "Tokyo"] cities = {"London", "Paris", "Tokyo"} cities = London, Paris, Tokyo cities = array("London", "Paris", "Tokyo")cities = (London, Paris, Tokyo)

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.