Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let angkot = [];
- let count = 0;
- tambahPenumpang('Andre', angkot);
- tambahPenumpang('Kevin', angkot);
- hapusPenumpang('Kevin', angkot);
- tambahPenumpang('Matthew', angkot);
- tambahPenumpang('Joko', angkot);
- tambahPenumpang('Matthew', angkot);
- hapusPenumpang('Andre2', angkot);
- function tambahPenumpang(namaPenumpang, angkot) {
- //check if there is undefined element in array
- let isUndefined = (element) => element == undefined;
- let checkIdx = angkot.findIndex(isUndefined);
- //check if there is duplicate element in array
- let isDuplicate = (element) => element == namaPenumpang;
- let checkName = angkot.findIndex(isDuplicate);
- //duplicate element check
- if (checkName >= 0) {
- console.log('- ' + namaPenumpang + ' sudah ada di dalam Angkot');
- }
- //check if first seat is empty or not
- else if (angkot.length == 0) {
- angkot[0] = namaPenumpang;
- }
- //undefined check
- else if (checkIdx >= 0) {
- angkot[checkIdx] = namaPenumpang;
- }
- //add passenger to array
- else {
- angkot.forEach(function(val, i, arr) {
- arr[count + 1] = namaPenumpang;
- });
- count++;
- };
- return angkot;
- };
- //remove passenger from array
- function hapusPenumpang(namaPenumpang, angkot) {
- //check is the passenger already seat or not
- let isAvailable = (element) => element == namaPenumpang;
- let checkPenumpang = angkot.findIndex(isAvailable);
- //check amount of passenger
- if (angkot.length == 0) {
- console.log('Angkot masih kosong!');
- }
- //remove the passenger
- else if (checkPenumpang >= 0) {
- angkot[checkPenumpang] = undefined;
- }
- //passenger isn't in the transportation
- else {
- console.log('- ' + namaPenumpang + ' tidak ada di dalam Angkot');
- };
- return angkot;
- }
- console.log(angkot.join(' - '));
- console.log("\nJumlah Penumpang Angkot : " + angkot.length);
Add Comment
Please, Sign In to add comment