Question

In: Computer Science

Using the data set as a pre-defined variable in your program, write code that uses the...

Using the data set as a pre-defined variable in your program, write code that uses the dataset to print the first names of people who have BOTH above average math grades AND below average age from the dataset.

The solutions for the textbook examples assume you are able to export in a framework like node.js, which is why in the data set I provide, I simply set the array of objects as a variable. Your code will be in the same file (treat it like any other array variable). You can do the same with the sample files the chapter provides. For example:

var ancestry = [
    {
       .. object ..
    },
    ...
]

// your code here

Requirements:

  • Cannot use any array type built-in functions except filter(), map(), and reduce().

var dataSet = [
{
"name": "Maura Glass",
"age": 60,
"math": 97,
"english": 63,
"yearsOfEducation": 4
},
{
"name": "James Gates",
"age": 55,
"math": 72,
"english": 96,
"yearsOfEducation": 10
},
{
"name": "Mills Morris",
"age": 26,
"math": 83,
"english": 77,
"yearsOfEducation": 10
},
{
"name": "Deena Morton",
"age": 57,
"math": 63,
"english": 63,
"yearsOfEducation": 10
},
{
"name": "Edith Roth",
"age": 38,
"math": 79,
"english": 94,
"yearsOfEducation": 10
},
{
"name": "Marva Morse",
"age": 31,
"math": 93,
"english": 78,
"yearsOfEducation": 9
},
{
"name": "Etta Potts",
"age": 48,
"math": 57,
"english": 93,
"yearsOfEducation": 7
},
{
"name": "Tate Moss",
"age": 22,
"math": 83,
"english": 64,
"yearsOfEducation": 8
},
{
"name": "Sanders Burris",
"age": 27,
"math": 65,
"english": 66,
"yearsOfEducation": 5
},
{
"name": "Latoya Malone",
"age": 35,
"math": 100,
"english": 100,
"yearsOfEducation": 5
},
{
"name": "Wade Foreman",
"age": 25,
"math": 76,
"english": 87,
"yearsOfEducation": 10
},
{
"name": "Miller Valentine",
"age": 31,
"math": 56,
"english": 89,
"yearsOfEducation": 6
},
{
"name": "Rita Olson",
"age": 53,
"math": 100,
"english": 52,
"yearsOfEducation": 6
},
{
"name": "Potter Newton",
"age": 29,
"math": 91,
"english": 75,
"yearsOfEducation": 5
},
{
"name": "Madeline Bartlett",
"age": 23,
"math": 60,
"english": 74,
"yearsOfEducation": 10
},
{
"name": "Tamara Tran",
"age": 46,
"math": 73,
"english": 78,
"yearsOfEducation": 4
},
{
"name": "Elena Evans",
"age": 43,
"math": 60,
"english": 82,
"yearsOfEducation": 10
},
{
"name": "Cote Merrill",
"age": 55,
"math": 86,
"english": 63,
"yearsOfEducation": 7
},
{
"name": "Madeleine Brennan",
"age": 52,
"math": 82,
"english": 88,
"yearsOfEducation": 4
},
{
"name": "Alford Weber",
"age": 38,
"math": 71,
"english": 85,
"yearsOfEducation": 4
},
{
"name": "Kirsten Daniel",
"age": 35,
"math": 86,
"english": 61,
"yearsOfEducation": 8
},
{
"name": "Melton Chan",
"age": 26,
"math": 55,
"english": 96,
"yearsOfEducation": 4
},
{
"name": "Mcmahon Woodward",
"age": 54,
"math": 56,
"english": 63,
"yearsOfEducation": 9
},
{
"name": "Helga Monroe",
"age": 29,
"math": 79,
"english": 92,
"yearsOfEducation": 5
},
{
"name": "Patricia Herrera",
"age": 46,
"math": 94,
"english": 99,
"yearsOfEducation": 10
},
{
"name": "Mccullough Lambert",
"age": 56,
"math": 65,
"english": 96,
"yearsOfEducation": 8
},
{
"name": "Haynes Davidson",
"age": 60,
"math": 86,
"english": 50,
"yearsOfEducation": 5
}
]

Solutions

Expert Solution

SOURCE CODE

var dataSet = [

{

"name": "Maura Glass",

"age": 60,

"math": 97,

"english": 63,

"yearsOfEducation": 4

},

{

"name": "James Gates",

"age": 55,

"math": 72,

"english": 96,

"yearsOfEducation": 10

},

{

"name": "Mills Morris",

"age": 26,

"math": 83,

"english": 77,

"yearsOfEducation": 10

},

{

"name": "Deena Morton",

"age": 57,

"math": 63,

"english": 63,

"yearsOfEducation": 10

},

{

"name": "Edith Roth",

"age": 38,

"math": 79,

"english": 94,

"yearsOfEducation": 10

},

{

"name": "Marva Morse",

"age": 31,

"math": 93,

"english": 78,

"yearsOfEducation": 9

},

{

"name": "Etta Potts",

"age": 48,

"math": 57,

"english": 93,

"yearsOfEducation": 7

},

{

"name": "Tate Moss",

"age": 22,

"math": 83,

"english": 64,

"yearsOfEducation": 8

},

{

"name": "Sanders Burris",

"age": 27,

"math": 65,

"english": 66,

"yearsOfEducation": 5

},

{

"name": "Latoya Malone",

"age": 35,

"math": 100,

"english": 100,

"yearsOfEducation": 5

},

{

"name": "Wade Foreman",

"age": 25,

"math": 76,

"english": 87,

"yearsOfEducation": 10

},

{

"name": "Miller Valentine",

"age": 31,

"math": 56,

"english": 89,

"yearsOfEducation": 6

},

{

"name": "Rita Olson",

"age": 53,

"math": 100,

"english": 52,

"yearsOfEducation": 6

},

{

"name": "Potter Newton",

"age": 29,

"math": 91,

"english": 75,

"yearsOfEducation": 5

},

{

"name": "Madeline Bartlett",

"age": 23,

"math": 60,

"english": 74,

"yearsOfEducation": 10

},

{

"name": "Tamara Tran",

"age": 46,

"math": 73,

"english": 78,

"yearsOfEducation": 4

},

{

"name": "Elena Evans",

"age": 43,

"math": 60,

"english": 82,

"yearsOfEducation": 10

},

{

"name": "Cote Merrill",

"age": 55,

"math": 86,

"english": 63,

"yearsOfEducation": 7

},

{

"name": "Madeleine Brennan",

"age": 52,

"math": 82,

"english": 88,

"yearsOfEducation": 4

},

{

"name": "Alford Weber",

"age": 38,

"math": 71,

"english": 85,

"yearsOfEducation": 4

},

{

"name": "Kirsten Daniel",

"age": 35,

"math": 86,

"english": 61,

"yearsOfEducation": 8

},

{

"name": "Melton Chan",

"age": 26,

"math": 55,

"english": 96,

"yearsOfEducation": 4

},

{

"name": "Mcmahon Woodward",

"age": 54,

"math": 56,

"english": 63,

"yearsOfEducation": 9

},

{

"name": "Helga Monroe",

"age": 29,

"math": 79,

"english": 92,

"yearsOfEducation": 5

},

{

"name": "Patricia Herrera",

"age": 46,

"math": 94,

"english": 99,

"yearsOfEducation": 10

},

{

"name": "Mccullough Lambert",

"age": 56,

"math": 65,

"english": 96,

"yearsOfEducation": 8

},

{

"name": "Haynes Davidson",

"age": 60,

"math": 86,

"english": 50,

"yearsOfEducation": 5

}

]

// using reduce to find the sum of math grades

var sum_math_grades = dataSet.reduce(function(acc, person){

return (acc + person["math"])

}, 0);

// using the sum to find average of math grades

var avg_math_grade = sum_math_grades / dataSet.length

// using reduce to find sum of age

var sum_age = dataSet.reduce(function(acc, person){

return (acc + person["age"])

}, 0);

// using the sum tofind average age

var avg_age = sum_age / dataSet.length

// using filter to get only yhose persons meeting the required condition

var final_array = dataSet.filter(function(person)

{

return person["math"] >= avg_math_grade && person["age"] <= avg_age

})

// using map to only store the first names of the person in an array

person_final = final_array.map(function(person){

return person["name"].split(" ")[0]

})

// printing the result

console.log(person_final)

OUTPUT


Related Solutions

Write an assembly program (Data and Code) that uses loop to read 10 numbers and output...
Write an assembly program (Data and Code) that uses loop to read 10 numbers and output the largest of those numbers, you can assume any length for those numbers. 80x86 assembly language
Write a program that uses the defined structure and all the above functions. Suppose that the...
Write a program that uses the defined structure and all the above functions. Suppose that the class has 20 students. Use an array of 20 components of type studentType. Other than declaring the variables and opening the input and output files, the function main should only be a collection of function calls. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the...
Change the code to sort list of strings without using any pre-defined functions Here is my...
Change the code to sort list of strings without using any pre-defined functions Here is my code: int n; String temp; Scanner in = new Scanner(System.in); System.out.print("Enter number of names you want to enter:"); n = in.nextInt(); String names[] = new String[n]; Scanner s1 = new Scanner(System.in); System.out.println("Enter all the names:"); for(int i = 0; i < n; i++){ names[i] = s1.nextLine(); } ***CHANGE THIS PART*** for (int i = 0; i < n; i++){ for (int j = i...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
Write and test a user-defined class (requiring conditions). Write an application (client) program that uses an...
Write and test a user-defined class (requiring conditions). Write an application (client) program that uses an instance(s) of a user-defined class. The federal income tax that a person pays is a function of the person's taxable income. The following table contains formulas for computing a single person's tax. Bracket Taxable Income Tax Paid 1 $22,100 or less 15% 2 More than $22,100 but $53,500 or less $3,315 plus 28% of the taxable income over $22,100 3 More than $53,500 but...
write a program for the microcontroller-msp430fr6989 using code composer studio not assembly language. write a code...
write a program for the microcontroller-msp430fr6989 using code composer studio not assembly language. write a code that transmits a single character and lights the red LED upon receiving that character. The board will "talk" to itself. The green LED should turn on whenever a message is sent and the LCD will display the message being received.
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end Use the readInput() method for the following input data Oranges: 10 for...
(Full Program)Write code that shows how deadlocks work. Then write code that shows a fix using...
(Full Program)Write code that shows how deadlocks work. Then write code that shows a fix using semaphores. (Full program)Write code showing the elevator algorithm. c++ Language
Write an assembly language program code to clear and set bit 7th and 19th in a...
Write an assembly language program code to clear and set bit 7th and 19th in a 32-bit variable called N.
Please write in Python code Write a program that stores the following data in a tuple:...
Please write in Python code Write a program that stores the following data in a tuple: 54,76,32,14,29,12,64,97,50,86,43,12 The program needs to display a menu to the user, with the following 4 options: 1 – Display minimum 2 – Display maximum 3 – Display total 4 – Display average 5 – Quit Make your program loop back to this menu until the user chooses option 5. Write code for all 4 other menu choices
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT