Contoh 1: Menggunakan split() dan join() // program to trim a string const string = ‘ Hello World ‘; const result = string.split(‘ ‘).join(”); console.log(result); Keluaran HelloWorld Pada program di atas, Itu split(‘ ‘) metode ini digunakan untuk membagi string menjadi elemen array individu. [“”, “”, “”, “”, “”, “”, “Hello”, “World”, “”, “”, “”, […]
Tag Archives: dari
Contoh: Dapatkan Item Acak Dari Array // program to get a random item from an array function getRandomItem(arr) { // get random index value const randomIndex = Math.floor(Math.random() * arr.length); // get random item const item = arr[randomIndex]; return item; } const array = [1, ‘hello’, 5, 8]; const result = getRandomItem(array); console.log(result); Keluaran ‘hello’ […]
Contoh 1: Ekstrak Nilai Menggunakan map() // program to extract value as an array from an array of objects function extractValue(arr, prop) { // extract value from property let extractedValue = arr.map(item => item[prop]); return extractedValue; } const objArray = [{a: 1, b: 2}, {a: 4, b: 5}, {a: 8, b: 9}]; // passing an […]
Contoh 1: Menggunakan indexOf() dan Push() // program to remove duplicate value from an array function getUnique(arr){ let uniqueArr = []; // loop through array for(let i of arr) { if(uniqueArr.indexOf(i) === -1) { uniqueArr.push(i); } } console.log(uniqueArr); } const array = [1, 2, 3, 2, 3]; // calling the function // passing array argument […]
Contoh 1: Menggunakan For Loop // program to remove item from an array function removeItemFromArray(array, n) { const newArray = []; for ( let i = 0; i < array.length; i++) { if(array[i] !== n) { newArray.push(array[i]); } } return newArray; } const result = removeItemFromArray([1, 2, 3 , 4 , 5], 2); console.log(result); Keluaran […]
Sebuah objek ditulis dalam a kunci / nilai pasangan. Itu kunci / nilai pasangan disebut properti. Sebagai contoh, const student = { name: ‘John’, age: 22 } Sini, name: ‘John’ dan age: 22 adalah dua sifat objek siswa. Contoh: Menghapus Properti Dari Objek // program to remove a property from an object // creating an […]
Contoh 1: Ubah huruf pertama menjadi Huruf Besar // program to convert first letter of a string to uppercase function capitalizeFirstLetter(str) { // converting first letter to uppercase const capitalized = str.charAt(0).toUpperCase() + str.slice(1); return capitalized; } // take input const string = prompt(‘Enter a string: ‘); const result = capitalizeFirstLetter(string); console.log(result); Keluaran Enter a […]
Contoh: Replace First Occurrence of a Character in a String // program to replace a character of a string const string = ‘Mr Red has a red house and a red car’; // replace the characters const newText = string.replace(‘red’, ‘blue’); // display the result console.log(newText); Keluaran Mr Red has a blue house and a […]
ASCII berdiri untuk Kode Standar Amerika untuk Pertukaran Informasi. ASCII adalah nilai numerik yang diberikan ke berbagai karakter dan simbol untuk disimpan dan dimanipulasi oleh komputer. Misalnya, nilai ASCII surat itu ‘SEBUAH’ aku s 65. Sumber: Bagan ASCII dari semua 127 karakter dalam JavaScript. Contoh 1: Nilai ASCII dari Karakter Menggunakan charCodeAt () // program […]
Contoh: Mengakses Elemen Array Menggunakan Pointer #include using namespace std; int main() { int data[5]; cout > data[i]; cout
- 1
- 2