Tag Archives: Dua

Program Javascript untuk Menghasilkan Angka Acak Antara Dua Angka

Jika Anda ingin menemukan bilangan bulat acak di antaranya min (termasuk) untuk maksimal (inklusif), Anda dapat menggunakan rumus berikut: Math.floor(Math.random() * (max – min + 1)) + min Contoh: Nilai Bilangan Bulat Antara Dua Angka // input from the user const min = parseInt(prompt(“Enter a min value: “)); const max = parseInt(prompt(“Enter a max value: […]

Program JavaScript Untuk Melakukan Persimpangan Antara Dua Array

Contoh 1: Lakukan Persimpangan Menggunakan Set // program to perform intersection between two arrays using Set // intersection contains the elements of array1 that are also in array2 function performIntersection(arr1, arr2) { // converting into Set const setA = new Set(arr1); const setB = new Set(arr2); let intersectionResult = []; for (let i of setB) […]

Program JavaScript untuk Membandingkan Elemen Dua Array

Contoh 1: Bandingkan Array Menggunakan JSON.stringify() // program to compare two arrays function compareArrays(arr1, arr2) { // compare arrays const result = JSON.stringify(arr1) == JSON.stringify(arr2) // if result is true if(result) { console.log(‘The arrays have the same elements.’); } else { console.log(‘The arrays have different elements.’); } } const array1 = [1, 3, 5, 8]; […]

Program JavaScript untuk Membuat Array Dua Dimensi

Contoh: Array Dua Dimensi Menggunakan untuk Loop // program to create a two dimensional array function twoDimensionArray(a, b) { let arr = []; // creating two dimensional array for (let i = 0; i< a; i++) { for(let j = 0; j< b; j++) { arr[i] = []; } } // inserting elements to array […]

Program JavaScript untuk Menggabungkan Dua Array dan Menghapus Item Duplikat

Contoh 1: Menggunakan concat() dan untuk Loop // program to merge and remove duplicate value from an array function getUniqueAfterMerge(arr1, arr2){ // merge two arrays let arr = arr1.concat(arr2); let uniqueArr = []; // loop through array for(let i of arr) { if(uniqueArr.indexOf(i) === -1) { uniqueArr.push(i); } } console.log(uniqueArr); } const array1 = [1, […]

Program JavaScript untuk Membandingkan Nilai Dua Tanggal

Contoh: Bandingkan Nilai Dua Tanggal // program to compare value of two dates // create two dates const d1 = new Date(); const d2 = new Date(); // comparisons const compare1 = d1 < d2; console.log(compare1); const compare2 = d1 > d2; console.log(compare2); const compare3 = d1 <= d2; console.log(compare3); const compare4 = d1 >= […]

Program JavaScript untuk Menggabungkan Properti Dua Objek

Contoh 1: Gabungkan Properti Dua Objek Menggunakan Object.assign() // program to merge property of two objects // object 1 const person = { name: ‘Jack’, age:26 } // object 2 const student = { gender: ‘male’ } // merge two objects const newObj = Object.assign(person, student); console.log(newObj); Keluaran { name: “Jack”, age: 26, gender: “male” […]

Program JavaScript untuk Menukar Dua Variabel

Contoh 1: Menggunakan Variabel Sementara //JavaScript program to swap two variables //take input from the users let a = prompt(‘Enter the first variable: ‘); let b = prompt(‘Enter the second variable: ‘); //create a temporary variable let temp; //swap variables temp = a; a = b; b = temp; console.log(`The value of a after swapping: […]

Program JavaScript untuk Menemukan Akar Pangkat Dua

Untuk menemukan akar kuadrat dari sebuah angka di JavaScript, Anda dapat menggunakan bawaan Math.sqrt() metode. Sintaksnya adalah: Math.sqrt(number); Di sini, Math.sqrt() metode mengambil angka dan mengembalikan akar kuadratnya. Contoh: Akar Pangkat Dua dari Sebuah Angka // take the input from the user const number = prompt(‘Enter the number: ‘); const result = Math.sqrt(number); console.log(`The square […]