skip to Main Content

I need to write a function that returns an array of objects.

The function will select the winners based on two factors: how much they paid, and their seat number. If both of these numbers are odd, the attendee wins a prize.

The function will be passed an array of numbers representing ticket costs as its only argument. The index position represents the seat number.

Each object should contain two keys. The first key is seat with a value of the odd index. The second key is ticketCost with a value of the ticket price at that index in the input array.

pickWinners([1, 3, 5, 7, 9, 11])
// should return [{seat: 1, ticketCost: 3}, {seat: 3, ticketCost: 7}, {seat: 5, ticketCost: 11} ]

This is my code:

function pickWinners(numbers) {
 
  let obj = {};
  let arr = [];
  for (let i = 0; i < numbers.length; i++) {
    if (i % 2 !== 0 && numbers[i] % 2 !== 0) {
      obj.seat = i;
      obj.ticketCost = numbers[i];


  
    }
    arr.push(obj);
  }
  return arr;
}

the code produces the same object in array i.e. pickWinners([1, 3, 5, 7, 9, 11])
[{seat: 5, ticketCost: 11}, {seat: 5, ticketCost: 1}, {seat: 5, ticketCost: 11} ]

If I’m not mistaken, my understanding is that this happens because I need to generate a new object name each time but I do not know how to do that when I don’t know how many winners there may be.

2

Answers


  1. You’re modifying the same object on every iteration, hence why you get this result. You just have to instantiate a new object on every iteration. You can even directly create the object and add it to the array in one line, like so:

    function pickWinners(numbers) {
        let arr = [];
        for (let i = 0; i < numbers.length; i++) {
            if (i % 2 !== 0 && numbers[i] % 2 !== 0) {
                arr.push({
                    seat: i,
                    ticketCost: numbers[i]
                });  
            }
    
        }
        return arr;
    }
    
    Login or Signup to reply.
  2. The error is that you’re setting the value of obj inside the if statement but appending it outside. That means if the condition isn’t met (seat number and ticket price are odd) It will take the value of obj which you set in the previous iteration of the array and append it.

    In simpler terms:
    when the if condtion: if (i % 2 !== 0 && numbers[i] % 2 !== 0) is met, it sets the variable obj. An example: {seat: 1, ticketCost: 7} then it exits the if statement and adds it to the array.

    The problem arises when the ticket cost and seat number aren’t odd. Since the arr.push(obj) isn’t inside the if statement it will push the value of obj which hasn’t changed ({seat: 1, ticketCost: 7} in this instance), causing a repitition.

    If you just want to append the winners to the array move the push inside the statement:

    for (let i = 0; i < numbers.length; i++) {
        if (i % 2 !== 0 && numbers[i] % 2 !== 0) {
          obj.seat = i;
          obj.ticketCost = numbers[i];
          arr.push(obj);
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search