# Javascript Sessions Part 1

Keiny Pacheco
·Jul 17, 2021·  Subscribe to my newsletter and never miss my upcoming articles

This is the first post from the Javascript sessions series. This may be useful if you are learning about arrays and algorithms using Javascript. Many of these algorithms can be useful for coding interviews as well.

As developers, it is necessary to practice our problem-solving skills. Sometimes, you can find many solutions to one single problem. So, if you want to share a different solution, you are welcome.

Problem 1: Find the K'th element of a list.

``````let fruits = ['🍉','🍐','🍏','🍒'];
const elementAt = (list, pos) => list[pos];
console.log(elementAt(fruits, 2)); //Should return 🍏
``````

Problem 2: Reverse a string. Fortunately, there are many ways to reverse a string. `"Hola" must be "aloH"`

In this solution, we simply use the reverse function. It's super easy!

``````// 1. Using reverse()
const reverseString = str => str.split('').reverse().join('');
console.log(reverseString("Hola")); //aloH
``````

It is possible to use a for loop, and you can use the traditional loop using an index. But in this case, we are going to use the simplified version instead. This version just loops each character of the string thanks to the "of" option.

``````// 2. Using a loop
const reverseString = str => {
let response = '';
for(let char of str){
response = char + response;
}
return response;
}

console.log(reverseString("hola")); //aloH
``````

Another alternative is to use our friend "reduce".

``````// 3. Using reduce()
const reverseString = str => str.split('').reduce((response, char) => char + response, '');
console.log(reverseString("hola")); //aloH
``````

Problem 3: Find out whether a string is a palindrome. A palindrome can be read forward or backward; e.g. (x a m a x).

First of all, we remove all non-alphanumeric characters like punctuation, spaces, and symbols. Then we convert the strings to lower case to check for palindromes.

``````function isPalindrome(str)
{
const paramString = str.toLowerCase().replace(/[^A-Za-z0-9]/g,'');
const reverseString = str.toLowerCase().replace(/[^A-Za-z0-9]/g,'').split('').reverse().join('');

return reverseString === paramString;
}

console.log(isPalindrome("Rotator")); //true
console.log(isPalindrome("I did, did I?")); //true
console.log(isPalindrome("Don't nod.")); //true
console.log(isPalindrome("Melon")); //false
``````

Problem 4: Find the coincidences of a word in a sentence.

When we work with problems like this, it is necessary to clean the text like in the previous problem. Then, we store each word from the sentence in an array and increment its value when we find the word again.

``````function coincidences(sentence, word)
{
sentence = sentence.toLowerCase().replace(/[^a-zA-Z0-9]/g,' ');
let result = 0;

if(sentence.includes(word)){
let words = sentence.split(' ');
let cont = {};

for(let word of words){
if(cont[word]){
cont[word]++;
}else{
cont[word] = 1;
}
}
result = cont[word];
}
return result;
}

let sentence = `JavaScript is a dynamic programming language that is used
for web development, in web applications and more`;
console.log(coincidences(sentence , "web")); //2
``````

Problem 5: Find out whether a string is a pangram. A pangram is a sentence or expression that uses all the letters of the alphabet.

Thanks to the every() function, we can check if every char from the string match with every char from the alphabet.

``````function isPangram(str)
{
let alphabet = 'abcdefghijklmnopqrstuvwxyz';
str = str.toLowerCase().replace(/^[a-z]+\$/g,'');

return alphabet.split('').every(char => str.includes(char));
}

console.log(isPangram("Pack my box with five dozen liquor jugs")); //true
``````

We have finished the first post of the Javascript series, I hope this can be useful for you. Don't forget to practice to master your skills as a developer.

If you like my work, feel free to support me