Contoh: Menggunakan impor/ekspor Mari kita buat file bernama modul.js (nama file bisa apa saja) dengan konten berikut: // program to include JS file into another JS file const message=”hello world”; const number = 10; function multiplyNumbers(a, b) { return a * b; } // exporting variables and function export { message, number, multiplyNumbers }; Untuk […]
Tag Archives: dalam
Contoh 1: Tambahkan Item ke Array Menggunakan splice() // program to insert an item at a specific index into an array function insertElement() { let array = [1, 2, 3, 4, 5]; // index to add to let index = 3; // element that you want to add let element = 8; array.splice(index, 0, element); […]
Contoh 1: Ganti Semua Contoh Karakter Menggunakan Regex // program to replace all instances of a character in a string const string = ‘Learning JavaScript Program’; const result = string.replace(/a/g, “A”); console.log(result); Keluaran LeArning JAvAScript ProgrAm Dalam contoh di atas, RegEx digunakan dengan replace() metode untuk mengganti semua instance karakter dalam string. /g menyatakan bahwa […]
Contoh 1: Hitung Jumlah Kunci dalam Objek Menggunakan for…in // program to count the number of keys/properties in an object const student = { name: ‘John’, age: 20, hobbies: [‘reading’, ‘games’, ‘coding’], }; let count = 0; // loop through each key/value for(let key in student) { // increase the count ++count; } console.log(count); Keluaran […]
Contoh 1: Periksa apakah Ada Kunci di Objek Menggunakan di Operator // program to check if a key exists const person = { id: 1, name: ‘John’, age: 23 } // check if key exists const hasKey = ‘name’ in person; if(hasKey) { console.log(‘The key exists.’); } else { console.log(‘The key does not exist.’); } […]
Lima huruf a, e, saya, o dan u disebut vokal. Semua huruf lainnya kecuali ini 5 vokal disebut konsonan. Contoh 1: Hitung Jumlah Vokal Menggunakan Regex // program to count the number of vowels in a string function countVowel(str) { // find the count of vowels const count = str.match(/[aeiou]/gi).length; // return number of vowels […]
Jika Anda memeriksa jumlah kemunculan ‘Hai’ dalam string ‘sekolah’, hasilnya adalah 2. Contoh 1: Periksa Kemunculan Karakter yang Menggunakan for Loop // program to check the number of occurrence of a character function countString(str, letter) { let count = 0; // looping through the items for (let i = 0; i < str.length; i++) { […]
Contoh: Mengurutkan Kata dalam Urutan Abjad // program to sort words in alphabetical order // take input const string = prompt(‘Enter a sentence: ‘); // converting to an array const words = string.split(‘ ‘); // sort the array elements words.sort(); // display the sorted words console.log(‘The sorted words are:’); for (const element of words) { […]
Bilangan bulat positif disebut bilangan Armstrong (urutan n) jika: abcd… = an + bn + cn + dn + … Dalam kasus bilangan Armstrong 3 digit, jumlah pangkat tiga dari setiap digit sama dengan bilangan itu sendiri. Sebagai contoh, 153 adalah nomor Armstrong karena: 153 = 1*1*1 + 5*5*5 + 3*3*3 Demikian pula, 1634 adalah […]
Bilangan prima adalah bilangan bulat positif yang hanya habis dibagi 1 dan dirinya sendiri. Sebagai contoh, 2, 3, 5, 7, 11 adalah beberapa bilangan prima pertama. Sebagai contoh, 4 bukan bilangan prima karena habis dibagi 1, 2, dan 4 itu sendiri. Ini adalah bilangan komposit. Contoh: Cetak Bilangan Prima // program to print prime numbers […]
- 1
- 2