JavaScript Coding Test Course, Gift Giving

Problem Description

You are in charge of delivering gifts to several friends. To deliver gifts to each friend, you need their unique ID. When given the ID of all friends and the ID of gifts, you need to determine whether the gifts can be delivered.

Problem Definition

Each friend is assumed to have the following information:

  • Friend’s ID
  • ID of the gift they want
  • Signal (whether this friend can receive the gift or not)

The input array contains information about several friends. Based on each friend’s ID and the ID of the gift they want, write a function to determine whether the gifts can be delivered accurately.

Input Format


    [
        { friendId: 1, giftId: 101, signal: true },
        { friendId: 2, giftId: 102, signal: false },
        { friendId: 3, giftId: 101, signal: true }
    ]
    

Output Format


    [
        { friendId: 1, giftId: 101, canReceive: true },
        { friendId: 2, giftId: 102, canReceive: false },
        { friendId: 3, giftId: 101, canReceive: true }
    ]
    

Solution Method

To solve this problem, you need to iterate through the array containing each friend’s information and determine if they can receive the gift. Basically, if the friend’s ID and the gift’s ID match, it’s assumed that they can receive the gift. However, if the friend’s signal value is false, they cannot receive the gift, so this needs to be taken into account.

Algorithm Explanation


    function canGiftsBeReceived(friends) {
        return friends.map(friend => {
            const canReceive = (friend.signal === true && friend.friendId === friend.giftId);
            return { ...friend, canReceive: canReceive };
        });
    }
    

The code above takes the given friends’ information and determines whether each friend can receive the gift, returning a new array.

Detailed Steps

  1. Function Definition: Define a function named canGiftsBeReceived that takes a parameter friends. This parameter is an array containing friends’ information.
  2. Iterate Through Array: Use the map method to iterate through the given friends array. Use a local variable named friend for each friend.
  3. Condition Check: For each friend, check if signal is true and if friendId matches giftId, saving the result in the canReceive value.
  4. Create Result Object: Create a new object based on each friend’s information. This object includes the existing friend information and the canReceive value.
  5. Return Result: Finally, return the transformed array.

Example Code


    const friends = [
        { friendId: 1, giftId: 101, signal: true },
        { friendId: 2, giftId: 102, signal: false },
        { friendId: 3, giftId: 101, signal: true }
    ];

    const result = canGiftsBeReceived(friends);
    console.log(result);
    

Result


    [
        { friendId: 1, giftId: 101, signal: true, canReceive: true },
        { friendId: 2, giftId: 102, signal: false, canReceive: false },
        { friendId: 3, giftId: 101, signal: true, canReceive: true }
    ]
    

The results above clearly show whether each friend can receive the gift. This method ensures safe delivery of gifts.

Conclusion

In this lecture, we explored a problem-solving method using basic arrays and objects. To solve algorithmic problems, it’s important to systematically analyze the problem and apply the appropriate algorithm. I hope this helps you tackle various problems using JavaScript.

© 2023 Algorithm Problem-Solving Course