Skip to content

Instantly share code, notes, and snippets.

@amiruddinid
Created May 12, 2025 05:42
Show Gist options
  • Save amiruddinid/0f34fb8d11f148085be2fb265b04ec9d to your computer and use it in GitHub Desktop.
Save amiruddinid/0f34fb8d11f148085be2fb265b04ec9d to your computer and use it in GitHub Desktop.
Malware analysis
function simulate(entries, s) {
  // 1) Buat salinan array agar tidak merubah data asli
  const result = [...entries];

  // 2) Iterasi setiap posisi di dalam array
  for (let i = 0; i < entries.length; i++) {
    // 3) Cek apakah i termasuk di s posisi pertama
    //    atau di s posisi terakhir
    if (i < s || i >= entries.length - s) {
      // 4) Jika ya, tandai sebagai “korup” dengan nilai -1
      result[i] = -1;
    }
    // 5) Kalau tidak (posisi di tengah), biarkan nilainya tetap
  }

  // 6) Kembalikan array yang sudah diproses
  return result;
}

Penjelasan Langkah demi Langkah

  1. Salin array input

    const result = [...entries];

    Spread syntax [...] membuat array baru dengan elemen yang sama, sehingga entries asli aman.

  2. Loop indeks

    for (let i = 0; i < entries.length; i++) {  }

    Variabel i berjalan dari 0 (elemen pertama) sampai entries.length - 1 (elemen terakhir).

  3. Tentukan “zona rawan”

    • i < s → posisi pertama sampai ke-s.
    • i >= entries.length - s → posisi terakhir sebanyak s.
  4. Tandai korup

    result[i] = -1;

    Setiap nilai di zona rawan diganti -1.

  5. Biarkan bagian tengah Jika i tidak di zona rawan, kode tidak melakukan apa‑apa → nilai asli tersimpan di result[i].

  6. Kembalikan hasil

    return result;

Contoh Penggunaan

const records = [4, 1, 3, 5, 4, 7, 9];
console.log(simulate(records, 3));
// Hasil: [-1, -1, -1, 5, -1, -1, -1]
  • Indeks 0–2 (tiga pertama) jadi -1.
  • Indeks 3 (tengah) tetap 5.
  • Indeks 4–6 (tiga terakhir) jadi -1.

Sekarang kamu punya implementasi “malware” yang hanya menyerang ujung-ujung array!

function simulate(entries, s) {
// 1) Buat salinan array agar tidak merubah data asli
const result = [...entries];
// 2) Iterasi setiap posisi di dalam array
for (let i = 0; i < entries.length; i++) {
// 3) Cek apakah i termasuk di s posisi pertama
// atau di s posisi terakhir
if (i < s || i >= entries.length - s) {
// 4) Jika ya, tandai sebagai “korup” dengan nilai -1
result[i] = -1;
}
// 5) Kalau tidak (posisi di tengah), biarkan nilainya tetap
}
// 6) Kembalikan array yang sudah diproses
return result;
}
const records = [ 4, 1, 3, 5, 4, 7, 9 ];
console.log(simulate(records, 3));
//Your company is analyzing malware that targets numerical record files stored in an array.
//The malware adjusts values at the array extremes using a window of size 's' as shown in the example below:
//const records = [4, 1, 3, 5, 4, 7, 9];
//console.log(simulate(records, 3));
// Output: [-1, -1, -1, 5, -1, -1, -1]
//Implement the simulate method so that the malware behavior is replicated for further study.
function simulate(entries, s) {
// Write your code here
return [];
}
const records = [ 4, 1, 3, 5, 4, 7, 9 ];
console.log(simulate(records, 3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment