Contoh: Konversi Tanggal ke Angka // program to convert date to number // create date const d1 = new Date(); console.log(d1); // converting to number const result = d1.getTime(); console.log(result); Keluaran Mon Nov 09 2020 10:52:32 GMT+0545 (Nepal Time) 1604898452084 Dalam contoh di atas, new Date() konstruktor digunakan untuk membuat objek tanggal. Itu new Date() […]
Tag Archives: JavaScript
Contoh: Menggunakan console.log() // program to write to console // passing number console.log(8); // passing string console.log(‘hello’); // passing variable const x = ‘hello’; console.log(x); // passing function function sayName() { return ‘Hello John’; } console.log(sayName()); // passing string and a variable const name=”John”; console.log(‘Hello ‘ + name); // passing object let obj = { […]
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”, “”, “”, “”, […]
Contoh: Dapatkan Dimensi Gambar // program to get the dimensions of an image const img = new Image(); // get the image img.src=”https://cdn.programiz.com/sites/tutorial2program/files/cover-artwork.png”; // get height and width img.onload = function() { console.log(‘width ‘ + this.width) console.log(‘height ‘+ this.height); } Keluaran width 1040 height 922 Pada program di atas, new Image() konstruktor digunakan untuk membuat […]
Contoh: Fungsi sebagai Parameter // program to pass a function as a parameter function greet() { return ‘Hello’; } // passing function greet() as a parameter function name(user, func) { // accessing passed function const message = func(); console.log(`${message} ${user}`); } name(‘John’, greet); name(‘Jack’, greet); name(‘Sara’, greet); Keluaran Hello John Hello Jack Hello Sara Dalam […]
Contoh 1: Menggunakan Number.isInteger() // program to check if a number is a float or integer value function checkNumber(x) { // check if the passed value is a number if(typeof x == ‘number’ && !isNaN(x)){ // check if it is integer if (Number.isInteger(x)) { console.log(`${x} is integer.`); } else { console.log(`${x} is a float value.`); […]
Antrian adalah struktur data yang mengikuti Masuk Pertama Keluar Pertama (FIFO) prinsip. Elemen yang ditambahkan pertama diakses terlebih dahulu. Ini seperti berada dalam antrian untuk mendapatkan tiket bioskop. Yang pertama mendapatkan tiket terlebih dahulu. Contoh: Implementasikan Antrian // program to implement queue data structure class Queue { constructor() { this.items = []; } // add […]
Tumpukan adalah struktur data yang mengikuti Terakhir Masuk Pertama Keluar (LIFO) prinsip. Elemen yang ditambahkan terakhir diakses terlebih dahulu. Ini seperti menumpuk buku Anda di atas satu sama lain. Buku yang Anda taruh akhirnya datang lebih dulu. Contoh: Implementasikan Tumpukan // program to implement stack data structure class Stack { constructor() { this.items = []; […]
Dalam pemrograman, fungsi overloading mengacu pada konsep di mana beberapa fungsi dengan nama yang sama dapat memiliki implementasi yang berbeda. Namun, dalam JavaScript, jika ada beberapa fungsi dengan nama yang sama, fungsi yang ditentukan terakhir akan dieksekusi. Fitur fungsi overloading dapat diimplementasikan dalam beberapa cara lain. Contoh 1: Menggunakan Pernyataan if/else-if // program to perform […]
Contoh: Hasilkan Rentang Karakter // program to generate range of numbers and characters function* iterate(a, b) { for (let i = a; i <= b; i += 1) { yield i } } function range(a, b) { if(typeof a === ‘string’) { let result = […iterate(a.charCodeAt(), b.charCodeAt())].map(n => String.fromCharCode(n)); console.log(result); } else { let result […]