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:~$
Question
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:~ bob@dylan:~
Solution
Here is a function in JavaScript that fulfills your requirements:
function getListStudents() {
return [
{ id: 1, firstName: 'Guillaume', location: 'San Francisco' },
{ id: 2, firstName: 'James', location: 'Columbia' },
{ id: 5, firstName: 'Serena', location: 'San Francisco' }
];
}
This function, when called, will return an array of objects. Each object represents a student and has three properties: id, firstName, and location. The students are listed in the order you specified.
Similar Questions
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:~$
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:~$
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:~$
2. A Course, Getters, and SettersmandatoryImplement a class named HolbertonCourse:Constructor attributes:name (String)length (Number)students (array of Strings)Make sure to verify the type of attributes during object creationEach attribute must be stored in an “underscore” attribute version (ex: name is stored in _name)Implement a getter and setter for each attribute.bob@dylan:~$ cat 2-main.jsimport HolbertonCourse from "./2-hbtn_course.js";const c1 = new HolbertonCourse("ES6", 1, ["Bob", "Jane"])console.log(c1.name);c1.name = "Python 101";console.log(c1);try { c1.name = 12;} catch(err) { console.log(err);}try { const c2 = new HolbertonCourse("ES6", "1", ["Bob", "Jane"]);}catch(err) { console.log(err);}bob@dylan:~$ bob@dylan:~$ npm run dev 2-main.js ES6HolbertonCourse { _name: 'Python 101', _length: 1, _students: [ 'Bob', 'Jane' ]}TypeError: Name must be a string ...TypeError: Length must be a number ...bob@dylan:~$
ES6 Iterate over array and find corresponding item in a different object
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.