# Some problems from Free Code Camp

# Reverse a String

let str = "oblivious";
let output = str.split("");
let len = str.length;
let outStr = "";
for (let i = len-1; i >= 0;i--) {
outStr += output[i];
}
console.log(outStr); // "suoivilbo"
1
2
3
4
5
6
7
8

# Factorialise a Number

Gotcha here is if zero is passed to the function, hence why always start with a 1 in the integers array as factorial of zero is 1.

function factorialize(num) {
let integers = ;
for (let n=2; n<=num; n++){
integers.push(n);
}
return integers.reduce((a, b) => a * b);
}
console.log(factorialize(5)); //120
1
2
3
4
5
6
7
8

# Longest word in a string

function findLongestWordLength(str) {
let regex = /\w+/g;
let words = str.match(regex);
let longest = 0;
for (let i = 0; i < words.length; i++) {
if (words[i].length > longest) {
longest = words[i].length;
}
}
return longest;
}
console.log(findLongestWordLength("The quick brown fox jumped over the lazy dog")); // 6
1
2
3
4
5
6
7
8
9
10
11
12

# Return largest numbers in arrays

Gotcha here was setting the largest variable to zero for each incoming subarray - when the numbers are positive this works, but if the largest number is a negative number, the zero never gets replaced, so instead it initialises to the first entry in the subarray.

function largestOfFour(arr) {
let newArr = [];
let largest = 0;
for (let i = 0; i < arr.length; i++) {
largest = arr[i];
for (let j = 0; j < arr[i].length; j++) {
if (arr[i][j] > largest) {
largest = arr[i][j];
}
}
newArr.push(largest);
}
return newArr;
}
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# Instead of endswith()

function confirmEnding(str, target) {
let lastOccurs = str.lastIndexOf(target);
if (lastOccurs + target.length == str.length) {
return true;
}
return false;
}
console.log(confirmEnding("Bastian", "n")) // true;
1
2
3
4
5
6
7
8

# Find first element of array that passes truth test.

Emphasis on find the first one, forgot to include the break; in the for loop to prevent looking for more matches and returning the last one. Meant to return undefined if no match, so don't initialise the variable.

function findElement(arr, func) {
let num;
for (let i = 0; i < arr.length; i++) {
if (func(arr[i])){
num = arr[i];
break;
}
}
return num;
}
findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; }); //first result is 8
findElement([1, 3, 5, 9], function(num) { return num % 2 === 0; }); //undefined
1
2
3
4
5
6
7
8
9
10
11
12

# Title Case a sentence

Not an ideal solution as not tested with punctuation, but it passed the test on FCC at least. Am sure it is a very poor solution though!

function titleCase(str) {
let regex = /[a-z\']+/ig;
let words = str.match(regex);
for (let i = 0; i < words.length; i++) {
words[i] = words[i].toUpperCase() + words[i].slice(1,words[i].length).toLowerCase()
}
let outStr = "";
for (let i = 0; i < words.length ;i++) {
outStr += words[i] + " ";
}
return outStr.trim();
}
console.log(titleCase("I'm a little tea pot")); //I'm A Little Tea Pot
1
2
3
4
5
6
7
8
9
10
11
12
13

# Using slice and splice to insert one array into another without changing either

function frankenSplice(arr1, arr2, n) {
let newArr = arr2.slice(0,n);
newArr.push(...arr1);
newArr.push(...arr2.slice(n,arr2.length));
return newArr;
}
console.log(frankenSplice([1, 2, 3], [4, 5, 6], 1)); //[ 4, 1, 2, 3, 5, 6 ]
1
2
3
4
5
6
7

# Where do I belong?

This problem is meant to find the first possible place in an array of numbers to correctly insert another number after the array has been sorted. The first gotcha is that sort by default is for strings so need to have a comparison function to tell sort how to treat them all as numbers. Solution looks inefficient but the logic I think flows from the unspoken rules - comments make it clear.

function getIndexToIns(arr, num) {
arr.sort((a, b) => a - b);
for (let i = 0; i < arr.length; i++) {
if (num == arr[i]) {
if (i==0){
return 0; //insert at start
}
return i-1; //insert before
}
if (num > arr[i] && num <= arr[i+1]) {
return i+1; //insert after
}
}
return arr.length; //insert at end
}
console.log(getIndexToIns([5, 3, 20, 3], 5));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# Chunk an array into groups

function chunkArrayInGroups(arr, size) {
let newArr = [];
let row = [];
for (let i = 0; i < arr.length; i++) {
if (row.length == size) {
newArr.push(row); //row is full so push
row = []; //reset row
}
row.push(arr[i]); //always add to the row
}
newArr.push(row); //push last row whether full or not.
return newArr;
}
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2)); //[[0, 1], [2, 3], [4, 5], [6, 7], ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14