You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 hello method in BaseActor only logs the name, not the full phrase "Hello, my name is [actor name]"
  • There's a typo in getActorByName: you wrote return actor[i] instead of return actors[i]
  • Some actor names don't match the required casing (e.g., "Leonardo di Caprio" vs. "Leonardo DiCaprio")
  • You're passing a hello property to BaseActor, but this should be a method defined in the constructor (not a static string)
  • The getAverageAge function hasn't been implemented yet
  • Storing properties inside a details object makes accessing them less straightforward (and doesn't match the requirement that each actor object has direct name, age, oscars properties)

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

  1. Direct Object Properties: Instead of nesting properties in a details object, we assign name, age, and oscars directly to this—this matches the requirement that each actor object has these properties, and makes accessing them simpler (e.g., actor.name instead of actor.details.name).
  2. Fixed Hello Method: Updated to log the full required greeting using template literals (you could also use string concatenation: "Hello, my name is " + this.name if you prefer).
  3. Typo Fix: Corrected actor[i] to actors[i] in getActorByName.
  4. Name Casing: Adjusted actor names to exactly match the required values.
  5. Added getAverageAge: Calculates the total age of all actors, then divides by the number of actors to get the average.
  6. Idiomatic Return Value: Returns null instead of false when no actor is found—this is standard practice in JavaScript for functions that search for objects.

内容的提问来源于stack exchange,提问作者dennisN86

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:14:50