Question

Consider a JavaScript object with keys first, last, and score, representing a student. The values of...

Consider a JavaScript object with keys first, last, and score, representing a student. The values of first and last are both strings (for first and last name respectively), while the value of score is an integer.

For example: {first: "John", last: "Smitt", score: 20}

Write a JavaScript function named maxName that, given an array of such objects, returns a string consisting of the full name (first and last) of the student with the largest score.

For example:

a = [
{first: "John", last: "Smith", score: 20},
{first: "Liza", last: "Hernandez", score: 50},
{first: "Mary", last: "Jones", score: 10}];
maxName(a) //=> "Liza Hernandez"

Homework Answers

Answer #1

Program Code Screenshot



Program Sample Console Input/Output Screenshot



Program Code to Copy

function maxName(obj){
  // assume largest to be the first score
  var largest_score_pos = 0;

  // compare this to all other scores
  for(var i=1;i<obj.length;i++){
    if(obj[largest_score_pos].score<obj[i].score){
      largest_score_pos=i;
    }
  }

  //largest_score_pos now contains the position of the person with largest score
  return obj[largest_score_pos].first+" "+obj[largest_score_pos].last;
}
a = [
{first: "John", last: "Smith", score: 20},
{first: "Liza", last: "Hernandez", score: 50},
{first: "Mary", last: "Jones", score: 10}];
console.log(maxName(a))
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Parse through the following json array in javascript to print out each employees first and last...
Parse through the following json array in javascript to print out each employees first and last name to the console: var json = [{"employees":[ { "firstName":"John", "lastName":"Doe" }, { "firstName":"Anna", "lastName":"Smith" }, { "firstName":"Peter", "lastName":"Jones" } ]}];
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). Extend...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). Extend the ItemToPurchase class per the following specifications              Private fields string itemDescription - Initialized in default constructor to "none" Parameterized constructor to assign item name, item description, item price, and itemquantity (default values of 0).             Public instance member methods setDescription() mutator & getDescription() accessor (2 pts) printItemCost() - Outputs the item name followed by the quantity, price, and subtotal printItemDescription() - Outputs the...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read code and all comments carefully): Student.h StudentList.h StudentList.cpp main.cpp This program: Creates a sorted linked list (student name and gpa) . The list is sorted in ascending order by name. Displays the list Read and understand this program, then do the following: finish writing Student.h and other files fix errors in StudentList.cpp Student.h: #ifndef STUDENT_H #define STUDENT_H //using namespace std; //<==== This statement //...
First Last Q0 Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 L0 L1 L2 L3...
First Last Q0 Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 L0 L1 L2 L3 L4 L5 L6 L7 L8 L9 P0 P1 P2 E0 E1 E2 FI ATT ------------------------------------------------------------------------------------------------------------------------------------------ Kevin Smith 90 100 100 100 98 97 87 100 85 87 89 100 100 100 100 90 100 98 90 100 98 98 98 90 90 98 88 0.00 Morgan Kelly 80 100 65 67 69 71 100 100 100 67 95 85 87 89 100 65 67...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...