<file_sep>// Built in functions

// 1. Math.PI
// Menghasilkan output berupa nilai pi International, 3.141592653589793
const pi = Math.PI;
console.log(`1. pi: ${pi}`);

// 2. Math.ceil
// Membulatkan keatas number yang tidak bulat
console.log(`2. ceil pi: ${Math.ceil(pi)}`);

// 3. Math.floor
// Membulatkan kebawah number yang tidak bulat
console.log(`3. floor pi: ${Math.floor(pi)}`);

// 4. Math.abs
// Menghasilkan nilai absolute, atau positif
console.log(`4. ${Math.abs(-256)}`);

// 5. Math.random
// Menghasilkan output berupa angka random. Limit dari angka yang dihasilkan diatur di parameter
// console.log(Math.random() * 3);
let dice = Math.floor(Math.random() * 6);
console.log(`5. random dice ${dice}`);

// 6. replace
// Merubah value yang match dengan parameter
let greet = 'Welcome to Indonesia';
greet = greet.replace('Indonesia', 'Uluwatu');
console.log(`6. ${greet}`);

// 7. Includes
// Jika dalam suatu variable terdapat potongan kata yang dicari, akan menghasilkan true
const name = 'Spain';
console.log(`7. includes: ${name.includes('pain')}`); //true

// 8. Map
// Mengakses dan melakukan operasi ke masing masing index dalam array
let arr = ['Jack', 'Jaqueline', 'Jason', 'Rahmat'];
const arrMap = arr.map(el => el += ' ehehehe');
console.log(`8. ${arrMap}`);

// 9. Some
// Memvalidasi jika beberapa index di array memenuhi kondisi yang diminta
let myFriendsAges = [18, 21, 23, 24, 26, 20];
const adult = (el) => el >= 18;
console.log(`9. ${myFriendsAges.every(adult)}`);

// 10. Every
// Memvalidasi jika seluruh index di array memenuhi kondisi yang diminta
console.log(`10. ${myFriendsAges.every(adult)}`);<file_sep>const isPalindrome = (word) => {
  if(!word || typeof word !== 'string') {
    return `The input may be empty or contains no string. Please re-check it!`
  }
  let reversed = '';
  for(let i = word.length - 1; i >= 0; i--) {
    reversed += word[i];
  }
  if(reversed.toLowerCase() === word.toLowerCase()) {
    return `${word} is Palindrome`
  } else {
    return `${word} is not a Palindrome`
  }
}

console.log(isPalindrome('Jakarta'));
console.log(isPalindrome('Malam'));
console.log(isPalindrome('101'));
console.log(isPalindrome());<file_sep>let data = {
  id: 1,
  name: "<NAME>",
  username: "Bret",
  email: "<EMAIL>",
  address: {
    street: "Kulas Light",
    suite: "Apt. 556",
    city: "Gwenborough",
    zipcode: "92998-3874",
  },
  phone: "1-770-736-8031 x56442",
  website: "hildegard.org"
}

// 1.
data = {
  ...data,
  name: "<NAME>",
  email: "<EMAIL>",
  hobbies: ["Programming", "Listening to Music"]
}

// 2.
const {street, city} = data.address;

console.log(data)
console.log(`Street: ${street},
City: ${city}`)<file_sep>const arkfood = (harga, voucher, jarak, pajak) => {
  const isValid = validate(harga, voucher, jarak, pajak);
  if(!isValid) {
    return console.log(`Please complete all required parameters properly.`);
  } else {
    const discount = promotionCheck(harga, voucher);
    const fare = (Math.ceil(jarak) <= 2) ? 5000 : ((Math.ceil(jarak) - 2) * 3000) + 5000;
    const tax = pajak ? 0.05 * harga : 0;
    const messages = {
      voucher: `0 "You are not using any voucher, or the voucher you try to use is invalid."`,
      tax: `0 "The restaurant doesn't charge any tax"`
    }
    return console.log(`
      Price        : ${harga}
      Discount     : ${(discount === 0) ? messages.voucher : discount}
      Delivery fee : ${fare}
      Tax          : ${(tax === 0) ? messages.tax : tax}
      SubTotal     : ${harga - discount + fare + tax}
    `)
  }
}

const validate = (harga, voucher, jarak, pajak) => {
  if(!harga || !voucher || !jarak || typeof pajak !== 'boolean' || harga <= 0) {
    return false
  } else if(typeof harga !== 'number' || typeof voucher !== 'string' || typeof jarak !== 'number') {
    return false
  } else {
    return true
  }
}

const promotionCheck = (harga, voucher) => {
  if(voucher.toUpperCase() === 'ARKAFOOD5' && harga >= 50000) {
    const discount = (harga >= 100000) ? 50000 : harga - (harga * 0.5);
    return discount
  } else if(voucher.toUpperCase() === 'DITRAKTIRDEMY' && harga >= 25000) {
    const discount = (harga >= 75000) ? 30000 : harga - (harga * 0.6);
    return discount
  } else {
    return 0
  }
}

arkfood(75000, 'arkafood5', 5.1, true)
arkfood(75000, 'false', 1, false)<file_sep>const biodata = {
  name: "<NAME>",
  age: "23",
  hobbies: ["Programming", "Listening to Music"],
  isMarried: false,
  schoolList: [
    {
      schoolName: "SMP Negeri 15 Jakarta",
      yearIn: 2009,
      yearOut: 2012,
      major: null
    },
    {
      schoolName: "SMK Negeri 8 Jakarta",
      yearIn: 2012,
      yearOut: 2015,
      major: "Pemasaran"
    },
  ],
  skills: [
    {
      skillName: "Javascript Programming",
      level: "Beginner"
    },
    {
      skillName: "Time Management",
      level: "Intermediate"
    }
  ],
  interesInCoding: true
}

console.log(biodata);<file_sep>const reverse = (word) => {
  if(!word || typeof word !== 'string') {
    return `The input may be empty or contains no string. Please re-check it!`
  } else {
    let reversed = '';
    for(let i = word.length - 1; i >= 0; i--) {
      reversed += word[i];
    }
    return reversed;
  }
}

console.log(reverse('Jakarta'));
console.log(reverse('Mesin tempur'));<file_sep>function cek(day) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const dataDay = ['senin', 'selasa', 'rabu', 'kamis', 'jumat'];
      let cek = dataDay.find(el => el === day.toLowerCase());
        if(cek) {
          resolve(`${day} adalah hari kerja`)
        } else {
          reject(new Error(`${day} bukan hari kerja`))
        }
    }, 3000)
  })
}

async function call(day) {
  try {
    console.log(`checking ...`);
    const result = await cek(day);
    console.log(result);  
  } catch(error) {
    console.log(error);
  }
}

call('sabtu')<file_sep>let names = ["Abigail", "Alexandra", "Alison", "Amanda", "Angela", "Bela", "Carol", 
  "Carolyn", "Deidre", "Diana", "Elizabeth", "Ella", "Faith", "Olivia", "Penelope"]

let searchName = (key, limit, callback) => {
  const isValid = callback(key, limit);
  if(!isValid) {
    return `Opps! You can only use string to define key & number to define limits.`
  } else {
    let result = [];
    for(let i in names) {
      if(limit > result.length) {
        if(names[i].toLowerCase().includes(key.toLowerCase(), 0)) {
          result.push(names[i])
        }
      }
    }
    if(result.length === 0) {
      return `Key '${key}' doesn't match with any data, try another key!`
    } else if(result.length === 1) {
      return `There is '${result}' mathces with key '${key}'`
    } else {
      return `There are '${result} match with key '${key}'`
    }
  }
}

const validator = (key, limit) => {
  if(typeof key !== 'string' || typeof limit !== 'number') {
    return false;
  } else {
    return true;
  }
}

console.log(searchName('abi', 5, validator));
console.log(searchName('an', 3, validator));
console.log(searchName('ali', 3, validator));<file_sep>// const cekGanjil = (num) => {
//   return new Promise((resolve, reject) => {
//     setTimeout(() => {
//       let isGanjil = num % 2;
//       if(isGanjil !== 0) {
//         resolve(num)
//       } else {
//         reject(new Error(`${num} bukan bilangan ganjil`))
//       }
//     }, 3000)
//   })
// }

// cekGanjil(2) 
//   .then((result) => { console.log(`${result} adalah ganjil`)})
//   .catch((error) => {console.log(error);})

const emailVerificator = (email, callback) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      let idxat;
      let idxdot;
      for(i in email) {
        if(email[i] === '@') {
          idxat = i;
        } else if(email[i] === '.') {
          idxdot = i;
        }
      }
      let provider = callback(email, idxat, idxdot);
      if(idxat && idxdot) {
        resolve(`Ini adalah email dari ${provider}`)        
      } else {
        reject(new Error(`Ini bukan email`))
      }
    }, 3000)
  })
}

const providerCheck = (email, at, dot) => {
  let provider = email.substring(at, dot)
  return provider;
} 

emailVerificator('<EMAIL>', providerCheck) 
  .then((result) => { console.log(result)})
  .catch((error) => { console.log(error);})
