JavaScript Coding Test Course, Sorting Cards

These days, many companies are evaluating applicants’ algorithm and problem-solving skills through coding tests. This time, we will take a look at the process of solving a problem with the topic of sorting cards. Sorting cards is one of the very simple yet frequently asked questions, requiring a fundamental understanding of algorithms and data structures.

Problem Description

We are trying to sort cards that have numbers written on them for a card game. The number of cards is N, and each card has an integer number from 1 to N. After sorting the given card array in ascending order, we need to print the final state of the card array.

Input Format

The first line contains the number of cards N (1 ≤ N ≤ 1000), and the second line contains the card numbers separated by spaces.

Output Format

Print the sorted card numbers separated by spaces.

Example Input

5
3 2 1 4 5

Example Output

1 2 3 4 5

Approach to the Problem

We can use the following approach to solve the problem.

  1. Data Collection: Collect the number of cards and the card numbers.
  2. Select a Sorting Algorithm: Use JavaScript’s built-in methods to sort the array.
  3. Output Data in the Required Format: Output the sorted array.

Code Writing

Now let’s write the code. Below is the JavaScript code to solve the card sorting problem.


function sortCards(cards) {
    return cards.sort((a, b) => a - b);
}

function main() {
    const n = parseInt(prompt("Enter the number of cards:")); // Prompt to enter the number of cards
    const cardsInput = prompt("Enter the card numbers (separated by spaces):"); // Enter card numbers
    const cards = cardsInput.split(" ").map(Number); // Convert the space-separated string to an integer array

    const sortedCards = sortCards(cards); // Call the sorting method
    console.log(sortedCards.join(" ")); // Output the sorted cards
}

main();

Code Explanation

Let’s explain each part of the code we wrote.

sortCards Function

The sortCards function takes an array of card numbers and returns it sorted in ascending order. It uses JavaScript’s built-in sort method, along with an arrow function to compare the sizes of the numbers.

Main Function

The main function performs the following tasks:

  • Uses prompt to input the number of cards.
  • Uses prompt again to input the card numbers and passes them as a string.
  • Uses the split method to convert the space-separated string into an array, and uses the map method to convert each element into a number.
  • Calls the sortCards function to get the sorted array.
  • Converts the result to a string using the join method and outputs it to the console.

Testing and Validation

We will test the code we wrote to verify that it works correctly with various inputs.

Test Case 1

Input:
5
3 2 1 4 5

Output:
1 2 3 4 5

Test Case 2

Input:
4
8 3 5 2

Output:
2 3 5 8

Conclusion

In this tutorial, we explored how to solve the card sorting problem using JavaScript’s array methods. Additionally, we had a chance to understand the approach and the role of each function through code writing. Problems like this are frequently asked in algorithm tests, so it is important to improve problem-solving skills through practice.

Additional Resources

For those who want to practice more advanced algorithm problems, please refer to the links below:

  • LeetCode – Provides various algorithm problems.
  • HackerRank – Provides algorithm and data structure problems.
  • Codewars – Offers problems of various difficulties and allows interaction with the community.

With that, we will conclude the JavaScript coding test course. Always strive to prepare for successful coding tests!