0
votes

I'm currently working on some practice for JavaScript and am really confused about what I am to do here. Any help would be appreciated!

Define a method named orderOfAppearance() that takes the name of a role as an argument and returns that role's order of appearance. If the role is not found, the method returns 0. Ex: orderOfAppearance("Elizabeth Swann") returns 3. Hint: A method may access the object's properties using the keyword this. Ex: this.title accesses the object's title property.

 // Code will be tested with different roles and movies
let movie = { 
   title: "Pirates of the Caribbean: At World's End",
   director: "Gore Verbinski",
   composer: "Hans Zimmer",
   roles: [ // Roles are stored in order of appearance
      "Jack Sparrow",
      "Will Turner",
      "Elizabeth Swann",
      "Hector Barbossa"
   ],
   orderOfAppearance: function(role) {

      /* Your solution goes here */
      if (!(role in this.roles)) {
         return 0;
      }
      return this.role;
      /*Solution ends here */
   }
};
2
"not to use a field named last(tail) in our program" Can you call it something else? It's not really clear what you're asking. - Andy Turner
No you are simply not allowed to have a variable that holds the value of the last node. My TA said you have to use first(head) to access the value of last(tail) - user14608777

2 Answers

0
votes

A doubly linked list has 2 links. Let's use common sense for a moment instead of blind deferral to whatever you think your TA said: "Doubly linked list" obviously implies "2 links", otherwise that'd be an extremely silly name, no?

So, there must be 2 links. You seem to be under the impression that 'you must not have a pointer to the tail' implies 'there must be only one link'. That can't be right, what with 'doubly linked list' and all.

The right answer is presumably that each node has 2 links, but these links aren't 'head and tail', but 'next' and 'previous'.

For a ringed list (where moving forward from the last element gets you back to the first, and moving backwards from the first element gets you to the last), the last node's "next" link goes back to the first node, and conversely, the first node's "previous" link goes to the last (so it points at the same thing a hypothetical 'tail' would point to).

However, that'd be only true for the first node in your linked list structure (that would be the only node for which 'prev' points at the tail).

Your TA either means 'have next/prev links, not next/tail links' as per above, or has no idea what they are talking about. Let's give em the benefit of the doubt ;) – as it's homework I'll leave the actual writing of the data structure, using a definition of DoublyLinkedNode<T> that includes fields DoublyLinkedNode<T> next, prev;, as an exercise for you.

0
votes

What I was looking for was to refer to the last use first.getPrev(), since its a circle the last is before the first.