Skip to content

Instantly share code, notes, and snippets.

'use strict;
const lotteryPromise = new Promise(function (resolve, reject) {
console.log('lottery draw is happening 🔮');
setTimeout(function () {
if (Math.random() >= 0.5) {
resolve('You WIN 💰');
} else {
reject(new Error('You lost your money 😔'));
@kironroy
kironroy / promises.js
Created August 18, 2023 19:54
Promises
Promise: An object that is used as a placeholder for the future result of an asynchronous operation.
⬇️
Promise: A container for asynchrounusly delivered value.
⬇️
Promise: A container for a future value.
👉 no call backs
👉 no unpredicatable results
👉 we can chain promises for a sequence of asynchrounous operations
'use strict';
// Your tasks:
// 1. Use a constructor function to implement a 'Car'. A car has a 'make' and a
// 'speed' property. The 'speed' property is the current speed of the car in
// km/h
// 2. Implement an 'accelerate' method that will increase the car's speed by 10,
// and log the new speed to the console
// 3. Implement a 'brake' method that will decrease the car's speed by 5, and log
// the new speed to the console
@kironroy
kironroy / classes.js
Last active July 26, 2023 00:06
Classes
class PersonCl {
constructor(fullName, birthYear) {
this.fullName = fullName;
this.birthYear = birthYear;
}
// all functions outside the constructor()
// will be on the prototype
// not on the object itself
const Car = function(make, speed) {
this.make = make;
this.speed = speed;
}
Car.prototype.accelerate = function () {
console.log(`${this.make} is accelerating at ${this.speed + 10} m/hr`);
};
@kironroy
kironroy / constructor.js
Last active July 25, 2023 18:29
constructor functions
'use strict';
// constructor functions
// 1. New {} is created
// 2. function is called, this = {}
// 3. {} linked to prototype
// 4. function automatically return {}
const Person = function (firstName, birthYear) {
@kironroy
kironroy / setTimeout.js
Created July 8, 2023 02:59
set time out
// setTimeout
const ingredients = ['olives', 'anchovies'];
const pizzaTimer = setTimeout(
(ing1, ing2) => console.log(`Here is your pizza with ${ing1} and ${ing2} 🍕`),
3000,
...ingredients
);
console.log('Waiting...');
@kironroy
kironroy / currencyTime.js
Created July 8, 2023 02:50
function for formatting currency, time
const formatCur = function (value, locale, currency) {
return new Intl.NumberFormat(locale, {
style: 'currency',
currency: currency,
}).format(value);
};
@kironroy
kironroy / internationalizing_numbers.js
Created July 8, 2023 02:30
internationalizing numbers
const options2 = {
// style: 'unit',
style: 'currency',
// unit: 'mile-per-hour',
unit: 'celsius',
currency: 'EUR',
// useGrouping: false,
};
@kironroy
kironroy / date
Created July 7, 2023 23:35
date operators
const calcdaysPassed = (date1, date2) =>
Math.abs((date2 - date1) / (1000 * 60 * 60 * 24));
const days1 = calcdaysPassed(new Date(2037, 3, 4), new Date(2037, 3, 14));
console.log(days1);