Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0e8a5f3

Browse files
committed
Refactor solutions
1 parent b8fb442 commit 0e8a5f3

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

‎README.md

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,7 @@ console.log(reverse(1534236469)); // 0
13991399
const reverse = x => {
14001400
const MAX = Math.pow(2, 31) - 1;
14011401
const MIN = -1 * Math.pow(2, 31);
1402-
let arr = Math.abs(x).toString().split('');
1402+
const arr = Math.abs(x).toString().split('');
14031403
const reversed = Math.sign(x) * Number(arr.reverse().join(''));
14041404
return reversed < MIN || reversed > MAX ? 0 : reversed;
14051405
};
@@ -1472,7 +1472,8 @@ const kidsWithCandies = (candies, extraCandies) => {
14721472
// Your solution
14731473
};
14741474

1475-
console.log(kidsWithCandies([2, 2, 1, 1, 2], 1)); // [true, false, false, false, false];
1475+
console.log(kidsWithCandies([12, 1, 12], 10)); // [true, false, true]
1476+
console.log(kidsWithCandies([4, 2, 1, 1, 2], 1)); // [true, false, false, false, false]
14761477
```
14771478
14781479
<details><summary>Solution</summary>
@@ -1530,9 +1531,9 @@ You are given an `m x n` integer grid `accounts`, where `accounts[i][j]` is the
15301531
Input: accounts = [[1,5],[7,3],[3,5]]
15311532
Output: 10
15321533
Explanation:
1533-
1st customer has wealth = 6
1534-
2nd customer has wealth = 10
1535-
3rd customer has wealth = 8
1534+
1st customer has wealth = 1 + 5 = 6
1535+
2nd customer has wealth = 7 + 3 = 10
1536+
3rd customer has wealth = 3 + 5 = 8
15361537
The 2nd customer is the richest with a wealth of 10.
15371538
```
15381539
@@ -1583,7 +1584,8 @@ const maximumWealth = accounts => {
15831584
15841585
The marketing team is spending way too much time typing in hashtags. Let's help them with our own Hashtag Generator! Here's the deal:
15851586
1586-
- It must start with a hashtag `#`
1587+
- It must start with a hashtag `#`.
1588+
- Ignore spaces in the input.
15871589
- All words must have their first letter capitalized.
15881590
- If the final result is longer than 140 chars it must return `false`.
15891591
- If the input or the result is an empty string it must return `false`.
@@ -1593,26 +1595,35 @@ const generateHashtag = str => {
15931595
// Your solution
15941596
};
15951597
1596-
console.log(generateHashtag('JavaScript')); // "#JavaScript"
1597-
console.log(generateHashtag('Do We have A Hashtag')); // "#DoWeHaveAHashtag"
1598+
console.log(generateHashtag('JavaScript')); // #JavaScript
1599+
console.log(generateHashtag('Do we have a Hashtag')); // #DoWeHaveAHashtag
1600+
console.log(generateHashtag(' Hello World ')); // #HelloWorld
1601+
console.log(generateHashtag('coding' + ' '.repeat(140) + 'for life')); // #CodingForLife
15981602
console.log(generateHashtag('')); // false
15991603
console.log(generateHashtag(' ')); // false
16001604
console.log(generateHashtag('a'.repeat(140))); // false
1601-
console.log(generateHashtag('a'.repeat(139))); // #Aaaaaaa...
1602-
console.log(generateHashtag('coding' + ' '.repeat(140) + 'for life')); // "#CodingForLife")
16031605
console.log(generateHashtag(' '.repeat(200))); // false
16041606
```
16051607
16061608
<details><summary>Solution</summary>
16071609
16081610
```js
16091611
const generateHashtag = str => {
1610-
let hashtag = str
1612+
const hashtag = str
16111613
.split(' ')
16121614
.reduce(
16131615
(tag, word) => tag + word.charAt(0).toUpperCase() + word.slice(1),
16141616
'#'
16151617
);
1618+
1619+
// Alternative solution
1620+
// const hashtag =
1621+
// '#' +
1622+
// str
1623+
// .split(' ')
1624+
// .map(ele => ele.charAt(0).toUpperCase() + ele.substring(1))
1625+
// .join('');
1626+
16161627
return hashtag.length === 1 || hashtag.length > 140 ? false : hashtag;
16171628
};
16181629
```
@@ -1683,11 +1694,11 @@ console.log(count('aba')); // { a: 2, b: 1 }
16831694
16841695
```js
16851696
const count = string => {
1686-
const result = {};
1687-
for (let char of string) {
1688-
result[char] = (result[char] || 0) + 1;
1697+
const frequency = {};
1698+
for (const char of string) {
1699+
frequency[char] = (frequency[char] || 0) + 1;
16891700
}
1690-
return result;
1701+
return frequency;
16911702
};
16921703
```
16931704
@@ -1763,7 +1774,7 @@ const getNumericValue = str => {
17631774
const offset = 'a'.charCodeAt();
17641775
const arr = [];
17651776
1766-
for (let char of str) {
1777+
for (const char of str) {
17671778
arr.push(char.charCodeAt() - offset);
17681779
}
17691780

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /