Skip to content

Commit 25da0e3

Browse files
committed
complete working with strings 3
1 parent ca575de commit 25da0e3

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

09-Data-Structure-Modern-Operators-and-Strings/README.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ console.log(airline.indexOf("Codo")); // output -1 (since result is not found)
661661
662662
### Slice Method
663663
664-
The `slice()` method returns a shallow copy of a portion of an arrayl or string into a new array object selected from `start` to `end`
664+
The `slice()` method returns a shallow copy of a portion of an array or string into a new array object selected from `start` to `end`
665665
666666
Syntax
667667
@@ -676,6 +676,7 @@ The importance of using these indexes is using the `slice()` method to get `inde
676676
**Start**
677677
678678
```js
679+
const airplane = "Dana Air Nigeria";
679680
console.log(airplane.slice(5));
680681
```
681682
@@ -859,7 +860,54 @@ const capitalizeName = function (name) {
859860
};
860861
```
861862
862-
## Summary
863+
### Padding
864+
865+
This allows us to add a number of elements to a string until a string is at a desired length.
866+
867+
```js
868+
const message = "Go to gate 23!";
869+
console.log(message.padStart(25, "+")); // +++++++++++Go to gate 23!
870+
```
871+
872+
This will add 11 plus symbols to the beginning of the string because using the `padStart()` method, we specified the string to be 25 characters long including the symbol and since the string `"Go to gate 23"` is 14 characters, the `padStart` method will fill it up with 11 + symbols.
873+
874+
If the string we want to pad is the same number of chracters on `padStart()` method, no value will be added to the string.
875+
876+
For example:
877+
878+
```js
879+
const message = "Hey";
880+
console.log(message.padStart(3, "+")); // Hey
881+
```
882+
883+
We also have the `padEnd()` method, which is the opposite of `padStart()`
884+
885+
Let's add 1 plus symbol at the end and beginning of a string.
886+
887+
```js
888+
console.log("Victor".padStart(10, "+").padEnd(14, "+")); // ++++Victor++++
889+
```
890+
891+
### Repeat
892+
893+
This method allows you to repeat a string multiple times.
894+
895+
```js
896+
const message2 = "Hello ";
897+
console.log(message2.repeat(3)); // Hello Hello Hello
898+
```
899+
900+
```js
901+
function planesInline(n) {
902+
console.log(`There are ${n} planes in line ${"✈️".repeat(n)}`);
903+
}
904+
905+
planesInline(10); // There are 10 planes in line ✈️✈️✈️✈️✈️✈️✈️✈️✈️✈️
906+
planesInline(3); // There are 3 planes in line ✈️✈️✈️
907+
planesInline(5); // There are 5 planes in line ✈️✈️✈️✈️✈️
908+
```
909+
910+
## Summary of the String Methods
863911
864912
- `toLowerCase()`: Converts all the alphabetic characters in a string to lowercase.
865913
- `toUpperCase()`: Converts all the alphabetic characters in a string to uppercase.
@@ -873,3 +921,4 @@ const capitalizeName = function (name) {
873921
- `endsWith()`: Returns true if the sequence of elements of searchString converted to a String is the same as the corresponding elements of this object (converted to a String) starting at endPosition – length(this). Otherwise returns false.
874922
- `padStart`: Pads the current string with a given string (possibly repeated) so that the resulting string reaches a given length. The padding is applied from the start (left) of the current string.
875923
- `padEnd`: Pads the current string with a given string (possibly repeated) so that the resulting string reaches a given length. The padding is applied from the end (right) of the current string.
924+
- `repeat()`: The repeat() method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.

09-Data-Structure-Modern-Operators-and-Strings/challenge.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,33 @@ for (const [minutes, event] of gameEvents) {
265265
: `[SECOND HALF] ${minutes}: ${event}`
266266
);
267267
}
268+
269+
("use strict");
270+
271+
/*
272+
---- Challenge ----
273+
Given a string of parenthesis, return the number of parenthesis you need to add to the string in order for it to be balanced
274+
275+
stringBalanced('()') => 0
276+
stringBalanced('(()') => 1
277+
stringBalanced('))()))))()') => 6
278+
stringBalanced(')))))') => 5
279+
*/
280+
281+
const stringBalanced = function (par) {
282+
let leftPar = par.split(")").length;
283+
let rightPar = par.split("(").length;
284+
285+
if (leftPar === rightPar) {
286+
return leftPar - rightPar;
287+
} else if (leftPar > rightPar) {
288+
return leftPar - rightPar;
289+
} else {
290+
return rightPar - leftPar;
291+
}
292+
};
293+
294+
console.log(stringBalanced("()"));
295+
console.log(stringBalanced("(()"));
296+
console.log(stringBalanced("))()))))()"));
297+
console.log(stringBalanced(")))))"));

09-Data-Structure-Modern-Operators-and-Strings/script.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ capitalizeName("victor eke");
638638
// Padding
639639
const message = "Go to gate 23";
640640
console.log(message.padStart(25, "+"));
641-
console.log("Victor".padStart(10, "+").padEnd(15, "+"));
641+
console.log("Victor".padStart(10, "+").padEnd(14, "+"));
642642

643643
const maskCreditCard = function (number) {
644644
const str = String(number);
@@ -647,3 +647,15 @@ const maskCreditCard = function (number) {
647647

648648
maskCreditCard(5493403400303010);
649649
maskCreditCard("4446482749251379");
650+
651+
// Repeat
652+
const message2 = "Hello";
653+
console.log(message2.repeat(3));
654+
655+
function planesInline(n) {
656+
console.log(`There are ${n} planes in line ${"✈️".repeat(n)}`);
657+
}
658+
659+
planesInline(10);
660+
planesInline(3);
661+
planesInline(5);

0 commit comments

Comments
 (0)