Array: How Sadhabas Used Arrays to Manage Their Trade

Array: How Sadhabas Used Arrays to Manage Their Trade

Long ago, there was a port town named Kalinga. This town was famous for its skilled Sadhaba traders, who sold everything from exotic spices to finely crafted gold and silver ornaments. Every Sadhaba kept records of the trades in thick dusty ledgers and every evening, they sat with an oil lamb checking the thick dusty record to count their stock, pending amounts, and so on. It was a slow and painful process.

The Arrival of the Mysterious Merchant

A mysterious traveler arrived during Asia's largest open-air trade fair, the Bali Jatra Festival. He wore a flowing robe with a glowing emblem on his chest, a golden "JS" symbol. The townspeople whispered, who is this man? He called himself Brendan Eich, the Coder, who helped businesses thrive.

Seeing the struggles of the Sadhabas, Brendan Eich smiled and said “You need a system a way to store and retrieve things quickly”. Let me introduce you to Array, the Helping Hand”.

The magic of Arrays

The Sadhabs gathered in curiosity. Brendan Eich took a chalk and drew a long wooden shelf on the ground and said Imagine, "Instead of searching your whole ledger, you store all your goods in one ordered row, just like this shelf. Each item has a numbered position, so finding anything is instant!" He then showed them the magic of Arrays:

  1. Pushing Items

    When a new item arrives, instead of flipping pages, you can place it at the end of the array.

const inventory = ["Rice", "Spices", "Silk"];
inventory.push("Gold"); //["Rice", "Spices", "Silk", "Gold"]
  1. Popping Items

    When the most recently added item needs to be sold, you can easily remove the item from the end of the array.

const inventory = ["Rice", "Spices", "Silk", "Gold"];
const soldItem = inventory.pop(); // ["Rice", "Spices", "Silk"]
  1. Finding Items in Seconds

    Instead of flipping pages, you can directly find an item based on a condition. Like the first item that counts more than 100.

const expensiveItem = prices.find(price => price > 100);
console.log(expensiveItem); // 150
  1. Sorting the Goods

    Using the Sort() method, you can arrange goods by price, weight, or name instantly.

const prices = [50, 200, 10, 150];
prices.sort((a, b) => a - b); // [10, 50, 150, 200]
  1. Filtering Out Expired Items

    Spoiled fruits? No problem. Arrays let them filter out bad items in no time.

const goods = ["Fresh Mango", "Spoiled Apple", "Good Rice", "Rotten Banana"];
const freshGoods = goods.filter(item => !item.includes("Spoiled") && !item.includes("Rotten"));
// ["Fresh Mango", "Good Rice"]
  1. Checking Availability

    Before selling, you can check if an item is in stock or not.

const inventory = ["Rice", "Spices", "Silk"];
inventory.includes("Silk"); // true
  1. Removing the First Item

    If the first item in line needs to be removed from your inventory, you can easily remove it.

const inventory = ["Rice", "Spices", "Silk"];
inventory.shift();
console.log(inventory); // ["Spices", "Silk"]
  1. Adding Items at the Start

    If a new high-demand item arrives, you can add that item to the first line of your inventory.

const inventory = ["Rice", "Spices", "Silk"];
inventory.unshift("Diamonds"
  1. Merging Inventories

    Multiple traders could merge their inventories.

let trader1 = ["Silver", "Bronze"];
let trader2 = ["Emerald", "Pearls"];
let mergedInventory = trader1.concat(trader2);
console.log(mergedInventory); // ["Silver", "Bronze", "Emerald", "Pearls"]
  1. Calculating the Total Value of Goods

    Instead of manually adding up prices, you can quickly calculate the total worth of your inventory, just like summing up all items on a ledger.

    const inventoryPrices = [500, 200, 300, 1000];
    const totalValue = inventoryPrices.reduce((sum, price) => sum + price, 0); //2000
    

    The Sadhabas couldn't believe their eyes. This was faster than any method they had ever seen. After that, the Kalinga Nagar transformed. Customers get their goods faster than ever, Sadhabas save time and avoid mistakes, and the town's economy boomed.