Skip to content

Commit

Permalink
feat: add problems
Browse files Browse the repository at this point in the history
  • Loading branch information
manishbisht committed Jul 5, 2023
1 parent b2019bd commit 07daf5f
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 0 deletions.
Empty file.
27 changes: 27 additions & 0 deletions BFE.dev/4. implement basic throttle().js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {Function} func
* @param {number} wait
*/
function throttle(func, wait) {
let waiting;
let params;

function callback() {
if (params) {
func(...params);
params = null;
}
}

return function (...args) {
params = args;
if (!waiting) {
callback();
}

waiting = setTimeout(() => {
waiting = null;
callback();
}, wait);
};
}
Empty file.
14 changes: 14 additions & 0 deletions BFE.dev/6. implement basic debounce().js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @param {Function} func
* @param {number} wait
*/
function debounce(func, wait) {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => {
func(...args);
timer = null;
}, wait);
};
}
24 changes: 24 additions & 0 deletions BFE.dev/7. implement debounce() with leading & trailing option.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @param {Function} func
* @param {number} wait
* @param {boolean} option.leading
* @param {boolean} option.trailing
*/
function debounce(func, wait, option = { leading: false, trailing: true }) {
let timer;
let lastArgs;
return function betterFunction(...args) {
lastArgs = args;
clearTimeout(timer);
if (option.leading && !timer && lastArgs) {
func(...lastArgs);
lastArgs = null;
}
timer = setTimeout(() => {
if (option.trailing && lastArgs) {
func(...lastArgs);
}
timer = null;
}, wait);
};
}
9 changes: 9 additions & 0 deletions BFE.dev/8. can you shuffle() an array?.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @param {any[]} arr
*/
function shuffle(arr) {
for (let i = 0; i < arr.length; i++) {
const j = i + Math.floor(Math.random() * (arr.length - i));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
22 changes: 22 additions & 0 deletions BFE.dev/9. decode message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {string[][]} message
* @return {string}
*/
function decode(message) {
let ans = "";
let i = 0;
let j = 0;
let direction = 1;
while (j < message.length && i < message[0].length) {
ans += message[j][i];
i++;
if (j === 0) {
direction = 1;
}
if (j + 1 === message.length) {
direction = -1;
}
j += direction;
}
return ans;
}
13 changes: 13 additions & 0 deletions Leetcode/Google/929. Unique Email Addresses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @param {string[]} emails
* @return {number}
*/
var numUniqueEmails = function (emails) {
const uniqueEmails = {};
emails.forEach(email => {
const [username, domainName] = email.split("@");
uniqueEmails[`${username.split("+")[0].replaceAll(".", "")}@${domainName}`] = 1;
});

return Object.keys(uniqueEmails).length;
};

0 comments on commit 07daf5f

Please sign in to comment.