-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2019bd
commit 07daf5f
Showing
8 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
BFE.dev/7. implement debounce() with leading & trailing option.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |