String & Array Methods I recently learned!
Tags: algorithms, codenewbie, javascript
Published: 2021-08-11 04:34:18

Hi y'all! Below are my most recent discovery of some  string and array manipulation methods! I stumbled upon these methods while I was working through my daily algos. I can't pass up the chance to share what I learned! So, here it goes!

### fill()
It fills up an array of undefined elements.
![fill method applied](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zweyevxahe3206ialqjs.png)
***

### repeat()
Takes a non-negative argument to determine the times of repetition and concats everything into one string. It is non-destructive and only makes a copy of the original string argument.
 ![Repeat method applied](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yceyea9qw9vmaj8caszc.png)

![Using string literals](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j161q9qisqeo692jnhsg.png)<figcaption>Using string literals or template literals</figcaption>

![repeat with float num](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dn0k2d12i4dptnr9m8lx.png)<figcaption>Float number arguments will be considered as integers.</figcaption>
***

### fromCodePoint() 
Static string method. Can't be used for a string object you created. Can be used for symbol matching

**Syntax:**
```javaScript
String.fromCodePoint(9731) - '☃' (_Yes I think it's a snowman too_)
```
***
### codePointAt()
You're maybe wondering how would you know which code point to use on the method beforehand. This is it!
This method returns a integer(non-negative) that is UTF-16 code point value. Takes in the index/position of the character from the string you want to know the code point value of.

**Syntax:**
```javaScript
let a = '★♲☃'
console.log(a.codePointAt(0))
//9733 
```
<figcaption>I think this can work really well in validations or comparison if needed.</figcaption>

***
### padEnd()
Yes! It pads the end of the string to reach the length you indicated in the argument. 

**Syntax:**
```javascript
let str = "Yup"
console.log(str.padEnd(8)+ "*")
// Yup     *
```
It can also take in a 2nd argument if you have a specific character or string to repeatedly pad your object.

**Syntax:**
```javascript
let str = "Yup"
console.log(str.padEnd(8, ".")+ "!")
// Yup.....!
```
Same principle goes for **[padStart()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart)**

***
### trim(), trimEnd(), trimStart()
Removes whitespaces from both ends of the string. While trimEnd and trimStart removes whitespaces from a specific side of the string.

trimEnd() & trimStart() are also known as **[trimLeft()](https://www.geeksforgeeks.org/javascript-trimstart-and-trimleft-method/) & [trimRight()](https://www.geeksforgeeks.org/javascript-trimend-and-trimright-method/)**.

You maybe wondering in what algorithm was I able to use some of these methods?

Try and see if you can solve this [Codewars](https://www.codewars.com/kata/5412509bd436bd33920011bc) Credit card mask challenge using some of the methods above!

Another one you can try is from CodeSignal, called Frame Generator. The goal is to write a function that takes in a number and returns an array of strings that would look like frame of asterisks.
![function call](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vjp44sl1p79fdn6q3ytz.png)<figcaption>Check out how I utilized some methods above to solve this! - [Github.com/tolentinoel](https://github.com/tolentinoel/algo_practice/blob/main/JS/frameGen.js)</figcaption>

Hope this is trivial! Until the next!