Interesting JavaScript Challenges

I have a lot of interest in both hardware and software. Last week, I stumbled upon a site called Free Code Camp. It allows anyone to sign up and go through a series of lessons/challenges in order to receive a “certificate” from them. There are three certificates at this point: Front End, Back End and Data Visualization. Needless to say, I created an account via GitHub and blazed through the first sections as a refresher!

My account: Vladimir Romanov.

The interesting sections came in the later sections of JavaScript. I’ve done a lot of JS in the past, but I’ve never gone through challenges like on FCC, so I wanted to share some of my interesting challenges and solutions on my site!

1. Palindrome Verification

Challenge: Return true if the given string is a palindrome. Otherwise, return false.

Solution:
function palindrome(str) {
var garbage = [' ',',','.','"\"','/','_','(',')','-'];
str= str.toLowerCase();
for(var i = 0;i < garbage.length;i++){ str= str.split(garbage[i]); str= str.join(''); } for(var i = 0; i <= str.length; i++){ if(str[i] != str[str.length-i-1]){ return false; } } return true; }

Walkthrough: the function I wrote, goes through the string passed to it and eliminates all of the "garbage" characters on the first pass. The second for loop is used to compare the current element with its "mirrored" one. In other words, you would compare the first character and the last, followed by the 2nd character and the second to last! This is how the str[i] != str[str.length-i-1] came from. The function is O(2n) = O(n) since it will be doing two linear passes through the given string.

2. Find the Longest Word in a String

Challenge: Return the length of the longest word in the provided sentence.

Solution:
function findLongestWord(str) {
var longest ="";
str = str.split(' ');
for(var i = 0;i longest.length) {
longest = str[i];
}
}
return longest.length;
}

Walkthrough: the above function is fairly straight forward. It breaks the string down into an array of words based on the space character; this is achieved with str.split(' ');. It then goes through the array and stores the longest string into the longest variable. At the end, the length of the final string is returned.

3. Return Largest Numbers in Arrays

Challenge: Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

Solution:
function largestOfFour(arr) {
var largestNum;
for(var i = 0; i < arr.length;i++){ largestNum = 0; for (var j = 0; j < arr[i].length;j++){ if(largestNum < arr[i][j]){ largestNum = arr[i][j]; } } arr[i] = largestNum; } return arr; }

Walkthrough: the function takes a 2D array as an input and returns the largest number within each sub-array. This is achieved by going through each sub-array and doing a comparison of the elements. The complexity of this function is O(m*n) where m is the size of the main array (4 was used for simplicity of the challenge) and n is the size of the sub-arrays.

Conclusion

I'm a big fan of challenges on the FFC website and will be continuing with the certifications on my spare time! I've enjoyed going through the JS section and solving all these coding puzzles. Hopefully, you were inspired to do the same; or were simply looking for solutions on Google.

Thank you,
Vlad