String & Array Methods I recently learned!
Ellaine Tolentino
Posted on August 11, 2021
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.
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.
fromCodePoint()
Static string method. Can't be used for a string object you created. Can be used for symbol matching
Syntax:
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:
padEnd()
Yes! It pads the end of the string to reach the length you indicated in the argument.
Syntax:
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:
let str = "Yup"
console.log(str.padEnd(8, ".")+ "!")
// Yup.....!
Same principle goes for 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() & trimRight().
You maybe wondering in what algorithm was I able to use some of these methods?
Try and see if you can solve this Codewars 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.
Hope this is trivial! Until the next!
Posted on August 11, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
May 21, 2020