@@ -1399,7 +1399,7 @@ console.log(reverse(1534236469)); // 0
1399
1399
const reverse = x => {
1400
1400
const MAX = Math .pow (2 , 31 ) - 1 ;
1401
1401
const MIN = - 1 * Math .pow (2 , 31 );
1402
- let arr = Math .abs (x).toString ().split (' ' );
1402
+ const arr = Math .abs (x).toString ().split (' ' );
1403
1403
const reversed = Math .sign (x) * Number (arr .reverse ().join (' ' ));
1404
1404
return reversed < MIN || reversed > MAX ? 0 : reversed;
1405
1405
};
@@ -1472,7 +1472,8 @@ const kidsWithCandies = (candies, extraCandies) => {
1472
1472
// Your solution
1473
1473
};
1474
1474
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]
1476
1477
` ` `
1477
1478
1478
1479
< details><summary> Solution< /summary>
@@ -1530,9 +1531,9 @@ You are given an `m x n` integer grid `accounts`, where `accounts[i][j]` is the
1530
1531
Input: accounts = [[1,5],[7,3],[3,5]]
1531
1532
Output: 10
1532
1533
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
1536
1537
The 2nd customer is the richest with a wealth of 10.
1537
1538
```
1538
1539
@@ -1583,7 +1584,8 @@ const maximumWealth = accounts => {
1583
1584
1584
1585
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:
1585
1586
1586
- - It must start with a hashtag `#`
1587
+ - It must start with a hashtag `#`.
1588
+ - Ignore spaces in the input.
1587
1589
- All words must have their first letter capitalized.
1588
1590
- If the final result is longer than 140 chars it must return `false`.
1589
1591
- If the input or the result is an empty string it must return `false`.
@@ -1593,26 +1595,35 @@ const generateHashtag = str => {
1593
1595
// Your solution
1594
1596
};
1595
1597
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
1598
1602
console.log(generateHashtag(' ' )); // false
1599
1603
console.log(generateHashtag(' ' )); // false
1600
1604
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")
1603
1605
console.log(generateHashtag(' ' .repeat(200))); // false
1604
1606
```
1605
1607
1606
1608
<details><summary>Solution</summary>
1607
1609
1608
1610
```js
1609
1611
const generateHashtag = str => {
1610
- let hashtag = str
1612
+ const hashtag = str
1611
1613
.split(' ' )
1612
1614
.reduce(
1613
1615
(tag, word) => tag + word.charAt(0).toUpperCase() + word.slice(1),
1614
1616
' # '
1615
1617
);
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
+
1616
1627
return hashtag.length === 1 || hashtag.length > 140 ? false : hashtag;
1617
1628
};
1618
1629
` ` `
@@ -1683,11 +1694,11 @@ console.log(count('aba')); // { a: 2, b: 1 }
1683
1694
1684
1695
` ` ` js
1685
1696
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;
1689
1700
}
1690
- return result ;
1701
+ return frequency ;
1691
1702
};
1692
1703
` ` `
1693
1704
@@ -1763,7 +1774,7 @@ const getNumericValue = str => {
1763
1774
const offset = ' a' .charCodeAt();
1764
1775
const arr = [];
1765
1776
1766
- for (let char of str) {
1777
+ for (const char of str) {
1767
1778
arr.push(char.charCodeAt () - offset);
1768
1779
}
1769
1780
0 commit comments