JavaScript演员对象数组及指定方法、函数实现求助
Fixing Your JavaScript Actor Array & Functions
Hey there! Let's work through your code to get it fully meeting the requirements you outlined. I'll start by pointing out the small issues in your existing code, then share the corrected version with explanations.
Issues in Your Current Code
- The
hellomethod inBaseActoronly logs the name, not the full phrase"Hello, my name is [actor name]" - There's a typo in
getActorByName: you wrotereturn actor[i]instead ofreturn actors[i] - Some actor names don't match the required casing (e.g., "Leonardo di Caprio" vs. "Leonardo DiCaprio")
- You're passing a
helloproperty toBaseActor, but this should be a method defined in the constructor (not a static string) - The
getAverageAgefunction hasn't been implemented yet - Storing properties inside a
detailsobject makes accessing them less straightforward (and doesn't match the requirement that each actor object has directname,age,oscarsproperties)
Corrected Full Code
// Define the BaseActor constructor with direct properties and required methods var BaseActor = function(actorDetails) { // Assign properties directly to the actor object this.name = actorDetails.name; this.age = actorDetails.age; this.oscars = actorDetails.oscars; // Hello method: logs the required greeting this.hello = function() { console.log(`Hello, my name is ${this.name}`); }; // hasMoreOscarsThan method: compares current actor's oscars to another actor's this.hasMoreOscarsThan = function(otherActor) { return this.oscars > otherActor.oscars; }; }; // Create the actors array with the exact required actor data var actors = [ new BaseActor({ name: "Leonardo DiCaprio", age: 41, oscars: 1 }), new BaseActor({ name: "Jennifer Lawrence", age: 25, oscars: 1 }), new BaseActor({ name: "Samuel L. Jackson", age: 67, oscars: 0 }), new BaseActor({ name: "Meryl Streep", age: 66, oscars: 3 }), new BaseActor({ name: "John Cho", age: 43, oscars: 0 }) ]; // Corrected getActorByName function: finds and returns the matching actor function getActorByName(name) { for (var i = 0; i < actors.length; i++) { if (actors[i].name === name) { return actors[i]; } } // Return null if no match is found (more idiomatic than false for object searches) return null; } // Implemented getAverageAge function: calculates the average age of all actors function getAverageAge() { var totalAge = 0; for (var i = 0; i < actors.length; i++) { totalAge += actors[i].age; } return totalAge / actors.length; } // Run your validation code to test everything var leo = getActorByName('Leonardo DiCaprio'); var jlaw = getActorByName('Jennifer Lawrence'); var jcho = getActorByName('John Cho'); var meryl = getActorByName('Meryl Streep'); console.log(jlaw.hasMoreOscarsThan(jcho)); // Output: true console.log(jlaw.hasMoreOscarsThan(meryl)); // Output: false console.log(leo.hasMoreOscarsThan(jlaw)); // Output: false meryl.hello(); // Output: "Hello, my name is Meryl Streep" leo.hello(); // Output: "Hello, my name is Leonardo DiCaprio" console.log(getAverageAge()); // Output: 48.4
Key Changes Explained
- Direct Object Properties: Instead of nesting properties in a
detailsobject, we assignname,age, andoscarsdirectly tothis—this matches the requirement that each actor object has these properties, and makes accessing them simpler (e.g.,actor.nameinstead ofactor.details.name). - Fixed Hello Method: Updated to log the full required greeting using template literals (you could also use string concatenation:
"Hello, my name is " + this.nameif you prefer). - Typo Fix: Corrected
actor[i]toactors[i]ingetActorByName. - Name Casing: Adjusted actor names to exactly match the required values.
- Added getAverageAge: Calculates the total age of all actors, then divides by the number of actors to get the average.
- Idiomatic Return Value: Returns
nullinstead offalsewhen no actor is found—this is standard practice in JavaScript for functions that search for objects.
内容的提问来源于stack exchange,提问作者dennisN86
相关产品推荐
相关产品推荐

