KUIS FUNGSIONAL PROGRAMMING

 KUIS FUNGSIONAL PROGRAMMING


Selesaikan kode js berikut:

const sampleProducts = [
  { id: 1, name: 'Laptop', category: 'Electronics', price: 1000 },
  { id: 2, name: 'Phone', category: 'Electronics', price: 500 },
  { id: 3, name: 'Shirt', category: 'Apparel', price: 50 },
  { id: 4, name: 'Shoes', category: 'Apparel', price: 80 },
  { id: 5, name: 'Watch', category: 'Accessories', price: 200 },
];

function getProductsByCategory(products, category) {
/**
   * TODO:
   * Gunakan metode array immutable untuk mengembalikan array produk yang termasuk dalam kategori yang diberikan.
   */
}
function findProductById(products, id) { /** * TODO: * Gunakan metode array immutable untuk mengembalikan produk dengan ID yang cocok. */
}
function calculateTotalPrice(products) { /** * TODO: * Gunakan metode array immutable untuk menghitung total harga semua produk. */
}
function applyDiscount(products, discount) { /** * TODO: * Gunakan metode array immutable untuk mengembalikan array baru, * di mana setiap produk memiliki harga yang sudah dikurangi dengan diskon yang diberikan. */
}
console.log(getProductsByCategory(sampleProducts, 'Electronics')); console.log(calculateTotalPrice(sampleProducts)); console.log(applyDiscount(sampleProducts, 10)); console.log(findProductById(sampleProducts, 3));


Berikut adalah penyelesaian kode menggunakan metode array immutable seperti filter, find, reduce, dan map:

javascript
const sampleProducts = [
  { id: 1, name: 'Laptop', category: 'Electronics', price: 1000 },
  { id: 2, name: 'Phone', category: 'Electronics', price: 500 },
  { id: 3, name: 'Shirt', category: 'Apparel', price: 50 },
  { id: 4, name: 'Shoes', category: 'Apparel', price: 80 },
  { id: 5, name: 'Watch', category: 'Accessories', price: 200 },
];

function getProductsByCategory(products, category) {
  // Menggunakan filter untuk menyaring data tanpa mengubah array asli
  return products.filter(product => product.category === category);
}

function findProductById(products, id) {
  // Menggunakan find untuk mencari satu objek berdasarkan kriteria
  return products.find(product => product.id === id);
}

function calculateTotalPrice(products) {
  // Menggunakan reduce untuk mengakumulasi total harga
  return products.reduce((total, product) => total + product.price, 0);
}

function applyDiscount(products, discount) {
  // Menggunakan map untuk membuat array baru dengan harga yang sudah dimodifikasi
  return products.map(product => ({
    ...product,
    price: product.price - (product.price * (discount / 100))
  }));
}

// Pengujian kode
console.log(getProductsByCategory(sampleProducts, 'Electronics')); 
console.log(calculateTotalPrice(sampleProducts)); 
console.log(applyDiscount(sampleProducts, 10)); 
console.log(findProductById(sampleProducts, 3)); 


Berikut Penjelasan Singkat:
  1. filter: Mencari semua item yang cocok dengan kategori.
  2. find: Mengambil satu objek spesifik berdasarkan ID-nya.
  3. reduce: Menjumlahkan seluruh price dari angka 0.
  4. map: Membuat salinan objek baru dengan harga terdiskon menggunakan spread operator (...product) agar data asli tetap terjaga (immutable).


Jika kode tersebut dieksekusi di konsol, berikut adalah hasilnya:
  1. getProductsByCategory(sampleProducts, 'Electronics')
    Mengembalikan array berisi produk dengan kategori 'Electronics':
    javascript
    [
      { id: 1, name: 'Laptop', category: 'Electronics', price: 1000 },
      { id: 2, name: 'Phone', category: 'Electronics', price: 500 }
    ]
    
    Use code with caution.
  • calculateTotalPrice(sampleProducts)
    Mengembalikan total penjumlahan harga semua produk:
    javascript
    1830
    
    Use code with caution.
  • applyDiscount(sampleProducts, 10)
    Mengembalikan array baru dengan harga yang telah dikurangi 10%:
    javascript
    [
      { id: 1, name: 'Laptop', category: 'Electronics', price: 900 },
      { id: 2, name: 'Phone', category: 'Electronics', price: 450 },
      { id: 3, name: 'Shirt', category: 'Apparel', price: 45 },
      { id: 4, name: 'Shoes', category: 'Apparel', price: 72 },
      { id: 5, name: 'Watch', category: 'Accessories', price: 180 }
    ]
    
    Use code with caution.
  • findProductById(sampleProducts, 3)
    Mengembalikan satu objek produk yang memiliki ID 3:
    javascript
    { id: 3, name: 'Shirt', category: 'Apparel', price: 50 }
    
    Use code with caution.
    Apakah Anda ingin menambahkan fungsi untuk memfilter produk berdasarkan rentang harga tertentu?



    Bersambung ke:



    Comments