Question

In: Computer Science

[Javascript] Create a function(returnObjectFromId(case, ...idNum)) to return the case Object(s) for a given idNum, or list...

[Javascript] Create a function(returnObjectFromId(case, ...idNum)) to return the case Object(s) for a given idNum, or list of idNums.

Calling with a single `idNum` value should return the case Object, and return NULL if an id value that's unknown is passed

returnObjectFromId(case, 84838) would return the Object in the cases Array with

an 'idNumber' of id, and use the .find() method of the cases Array to locate items by idNumber.

returnObjectFromId(cases, -23298312) would return null.

returnObjectFromId(cases, 161020, 161021) would return an Array of case Objects

with two elements, matching the id values. We don't add anything to the returned Array,

if any of the ids in the list are unknown.

As an example, the following function would return an Array of 2 case Objects, ignoring the unknown

id(-23298312):

returnObjectFromId(cases, 231321, 241249, -23298312) would return an Array of 2 cases.

example of case object:

case = {

"idNumber": 112319,

"Reported Date": "2020-08-15",

"Episode Date": "2020-07-12",

},

{

"idNumber": 132421,

"Reported Date": "2020-08-12",

"Episode Date": "2020-07-19",

},

...etc

Solutions

Expert Solution

Hello,

I have created returnObjectFromId function based on your requirement. I have used sample data as provided by you in question. Tested the code with valid and invalid inputs. Attached returnObjectFromId function and output screenshots. Please let me know if any changes required.

returnObjectFromId function:

// Function to return objects which matches id in the cases list
function returnObjectFromId(cases, ...idNum) {
    // Declared an empty array object to store output
    var resultIdArr = [];
    // Iterating through the idNum array
    idNum.forEach(idNumber => {
        // Identify case with idNumber by using find method in array prototype
        const caseFound = cases.find(element => element.idNumber === idNumber);
        // If case found with idNumber then add it to result array
        if(caseFound) {
            resultIdArr.push(caseFound);
        }
    });
    // If result array length is equal to 0 that return null means there are no case found with given list of id numbers
    if(resultIdArr.length == 0) {
        return null;
    } else {
        // Else return result array
        return resultIdArr;
    }
}

Test with valid input:

// Sample cases data based data available in question 
var casesData = [{
    "idNumber": 112319,
    "Reported Date": "2020-08-15",
    "Episode Date": "2020-07-12",
    },
    {
    "idNumber": 132421,
    "Reported Date": "2020-08-12",
    "Episode Date": "2020-07-19",
    }];
    
// Method call returnObjectFromId function with cases data and valid and invalid id numbers
 var result = returnObjectFromId(casesData, 112318, 112319, 132421);
// Printing the result
 console.log(result);

Output when valid input:

Test with invalid input:

// Sample cases data based data available in question 
var casesData = [{
    "idNumber": 112319,
    "Reported Date": "2020-08-15",
    "Episode Date": "2020-07-12",
    },
    {
    "idNumber": 132421,
    "Reported Date": "2020-08-12",
    "Episode Date": "2020-07-19",
    }];
    

// Method call returnObjectFromId function with cases data and invalid id numbers
var resultNull = returnObjectFromId(casesData, 112318, -112319, -132421);
// Printing the result
console.log(resultNull);

Output when invalid input:


Related Solutions

[Javascript] Create a function that gets the total amount and percentage of covid case data for...
[Javascript] Create a function that gets the total amount and percentage of covid case data for each Age Group in the covid cases data set(which contains the property "Age Group" in each case array). Age Groups: 'younger than 18', 'between 19 to 28 Years', 'between 29 to 38 Years','between 39 to 48 Years','between 49 to 58 Years','between 59 to 68 Years','between 69 to 78 Years','between 79 to 88 Years', and'older than 89' the function getSummaryOfAges(data) should return an Object with...
JAVASCRIPT: - Please create an object (grade) with 10 names and 10 grades. - Create a...
JAVASCRIPT: - Please create an object (grade) with 10 names and 10 grades. - Create a method (inputGrade) that can put a name and a grade to the grade object. - Create another method (showAlltheGrades) to show all the grade in that object. - Create the third method (MaxGrade) that can display the maximum grade and the student name. - Using “prompt” and inputGrade method input 10 student names and their grades. - Display all the grades and names by...
JAVASCRIPT: - Please create an object (grade) with 10 names and 10 grades. - Create a...
JAVASCRIPT: - Please create an object (grade) with 10 names and 10 grades. - Create a method (inputGrade) that can put a name and a grade to the grade object. - Create another method (showAlltheGrades) to show all the grade in that object. - Create the third method (MaxGrade) that can display the maximum grade and the student name. - Using “prompt” and inputGrade method input 10 student names and their grades. - Display all the grades and names by...
Javascript problem: Given an argument of a list of book objects: Example test case input: {...
Javascript problem: Given an argument of a list of book objects: Example test case input: { "title" : "Book A", "pages": "50", "next": { "title": "Book B", "pages": 20, "next": null } } create a recursive function to return the total number total # of pages read in the list. If there are no books in the list, return 0.
Create a JavaScript function that will collect the information from the form and verify that it...
Create a JavaScript function that will collect the information from the form and verify that it is the correct type and that there are no blank textboxes. Save and test the file to ensure that the textbox information is collected and the script is working correctly. Use the onclick event within the submit button to call this function. Output validation error messages by writing directly to the with the id of "results." (You may want to use alert boxes for...
Javascript Problem: List Reverse Given a list: var list = { value: 1, next: { value:...
Javascript Problem: List Reverse Given a list: var list = { value: 1, next: { value: 2, next: { value: 3, next: null } } }; Reverse the order of the list so that it looks like: var list = { value: 3, next: { value: 2, next: { value: 1, next: null } } }; Use the following shell if needed: //assignment1.js function reverseList(list) { // your code here ... return reversedList; } Example Test Case(s): Arguments: { value:...
If it needs more information be specific. JavaScript Functions Create the makeBranches() function that will be...
If it needs more information be specific. JavaScript Functions Create the makeBranches() function that will be used to append node branches to the node tree diagram. The function will have two parameters named treeNode and nestedList. The treeNode parameter stores the current node from the source article and the nestedList parameter stores the structure of the node tree displayed in the web page. Add the commands described in the steps below. Each time the makeBranches() function is called, it is...
JAVASCRIPT Create an array of 5 objects named "movies" Each object in the movies array, should...
JAVASCRIPT Create an array of 5 objects named "movies" Each object in the movies array, should have the following properties: Movie Name Director Name Year Released WasSuccessful (this should be a boolean and at least 2 should be false) Genre Loop through all of the objects in Array If the movie is successful, display all the movie information on the page. These movies were a success: Title: Forrest Gump Year Realeased: 1994 Director: Robert Zemeckis Genre: Comedy
JavaScript Given the following object, log every property name and value to the console using a...
JavaScript Given the following object, log every property name and value to the console using a loop. let myObj = { id: 12 name: 'My Object', class: 'obj', height: 65, likeJavascript: true, data: [1, 53, 23] };
For this assignment you need to create a ToDo list using Javascript, along with HTML and...
For this assignment you need to create a ToDo list using Javascript, along with HTML and CSS. Begin by creating a HTML page called todo.html. Then create a Javascript file called todo.js and link it in to the HTML page using a script tag. All Javascript for the assignment must be in the separate file. (For CSS, feel free to include styles in a style block at the top of the HTML page, or to link in CSS from a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT