1

I just got the result "[object Object]'s score is 0" printed on the terminal. The result 27 was all fine until I separated the function into a return object.

  1. How do I get 27 if I have to return an object?
  2. How do I get "alex" printed on the console.log instead of [object Object]?

const alex = {
  first: [1, 2, 9, 8],
  second: [3],
  third: [0, 0, 0, 1, 3]
};
const gordon = {
  first: [3],
  second: [2, 2, 4, 5, 6, 6, 7, 8]
}

function createPlayer(object) {
  let score = 0;
  return {
    add: function() {
      for (const key in object) {
        for (const item in object[key]) {
          score += object[key][item]
        }
      }
    },
    result: function() {
      return `${object}\'s score is ${score}`
    }
  }
}
createPlayer(alex).add()
console.log(createPlayer(alex).result())

1
  • you log createPlayer(alex).result() ... you haven't added anything yet Commented Sep 16, 2020 at 6:24

2 Answers 2

2

const alex = {
  first: [1, 2, 9, 8],
  second: [3],
  third: [0, 0, 0, 1, 3]
};
const gordon = {
  first: [3],
  second: [2, 2, 4, 5, 6, 6, 7, 8]
}

function createPlayer(object, name) {
  let score = 0;
  return {
    add: function() {
      for (const key in object) {
        for (const item in object[key]) {
          score += object[key][item]
        }
      }
      return this; // <<<<<
    },
    result: function() {
      return `${name}\'s score is ${score}`
    }
  }
}
console.log(createPlayer(alex, 'Alex').add().result())

Sign up to request clarification or add additional context in comments.

4 Comments

This is a smart way doing it. Except, I do not understand the meaning of putting"return this" there?
so you can chain the methods ... .add() returning this means you can .add().result()
yes, it does more than chaining the methods. I find that without "return this", even if I .log it separately, the .result method will not show 27. It is like it has no connection with the .add method's result. it is hard to understand for beginner. Do you know what topic I should look into to understand this concept?
method chaining? I don't know, been years since I did javascript 101
0

You would not show alex for an object named alex

You might mean this

const alex = {
  Name: "Alex",
  first: [1, 2, 9, 8],
  second: [3],
  third: [0, 0, 0, 1, 3]
};
const gordon = {
  Name: "Gordon",
  first: [3],
  second: [2, 2, 4, 5, 6, 6, 7, 8]
}

function createPlayer(object) {
  let score = 0;
  return {
    add: function() {
      for (const key in object) {
        if (key!=="Name") {
          for (const item in object[key]) {
            score += object[key][item]
          }
        }   
      }
    },
    result: function() {
      return `${object.Name}\'s score is ${score}`
    }
  }
}
const player1 = createPlayer(alex)
player1.add()
console.log(player1.result())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.