Basic Algorithm Scripting Flashcards
Reverse string (code)
function reverseString(str) { str = str.split("").reverse().join(""); return str; }
Factorial (code)
function factorialize(num) { if(num == 0){ //the base case return 1; } else { num = factorialize(num-1) * num; } //console.log(num); return num; }
Find the longest length word in a string (code)
function findLongestWordLength(str) { var words = str.split(' '); var maxLength = 0;
for (var i = 0; i < words.length; i++) { if (words[i].length > maxLength) { maxLength = words[i].length; } }
return maxLength;
}
Return largest number in array of arrays (code)
function largestOfFour(arr) { var largest = []; for (let i = 0; i < arr.length; i++){ var maxNum = 0; for (let j = 0; j < arr[i].length; j++){ if (arr[i][j] > maxNum){ maxNum = arr[i][j]; } } largest.push(maxNum); }
return largest;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Check if a string (first argument, str) ends with the given target string (second argument, target).
function confirmEnding(str, target) { let testRegex = new RegExp(target+"$","i"); return testRegex.test(str); // OR A BETTER WAY IS TO DO // return str.slice(str.length - target.length) === target; }
console.log(confirmEnding(“Bastian”, “n”));
Repeat a string (first arg) however many times (second arg)
function repeatStringNumTimes(str, num) { if (num < 0){ return ""; } else { let finalStr = ''; while(num > 0){ finalStr += str; num--; } return finalStr; } }
Truncate a string if it’s longer than length given
function truncateString(str, num) {
if(str.length > num){ return str.slice(0,num) + "..."; } else { return str; } }
truncateString(“A-tisket a-tasket A green and yellow basket”, 8);
Create a function that looks through an array (first argument) and returns the first element in the array that passes a truth test (second argument as function). If no element passes the test, return undefined (code)
function findElement(arr, func) {
for(let i = 0; i < arr.length; i++){ let num = func(arr[i]); if (num == true){ return arr[i]; } } }
findElement([1, 2, 3, 4], num => num % 2 === 0);
Check if a value is classified as a boolean primitive. Return true or false.
Boolean primitives are true and false.
function booWho(bool) { if (typeof bool === 'boolean'){ return true } else { return false; }
}
booWho(null);
Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.
For the purpose of this exercise, you should also capitalize connecting words like “the” and “of”. (code)
function titleCase(str) { return str.toLowerCase().replace(/(^|\s)\S/g, L => L.toUpperCase()); }
titleCase(“I’m a little tea pot”);
You are given two arrays and an index.
Use the array methods slice and splice to copy each element of the first array into the second array, in order.
Begin inserting elements at index n of the second array.
Return the resulting array. The input arrays should remain the same after the function runs.
function frankenSplice(arr1, arr2, n) { let newArr = arr2.slice(); for(let i = 0; i < arr1.length; i++){ newArr.splice(n+i,0,arr1[i]);
}
return newArr;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
Remove all falsy values from an array.
Falsy values in JavaScript are false, null, 0, “”, undefined, and NaN.
Hint: Try converting each value to a Boolean.
function bouncer(arr) { let newArr = []; for (let i = 0; i < arr.length; i++) { if (!!arr[i]) { newArr.push(arr[i]); }; }
return newArr;
}
bouncer([7, “ate”, “”, false, 9]);
Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.
For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).
Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).
function getIndexToIns(arr, num) { return arr.filter(val => num > val).length; }
Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
For example, [“hello”, “Hello”], should return true because all of the letters in the second string are present in the first, ignoring case.
The arguments [“hello”, “hey”] should return false because the string “hello” does not contain a “y”.
Lastly, [“Alien”, “line”], should return true because all of the letters in “line” are present in “Alien”.
function mutation(arr) {
var test = arr[1].toLowerCase();
var target = arr[0].toLowerCase();
for (var i = 0; i < test.length; i++) {
if (target.indexOf(test[i]) < 0) return false;
}
return true;
}
mutation([“hello”, “Hello”]);
mutation([“hello”, “hey”]) ;
mutation([“zyxwvutsrqponmlkjihgfedcba”, “qrstu”]);
Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.
function chunkArrayInGroups(arr, size) { // Break it up. var arr2 = []; for (var i = 0; i < arr.length; i += size) { arr2.push(arr.slice(i, i + size)); } return arr2; }
chunkArrayInGroups([“a”, “b”, “c”, “d”], 2);
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3);
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3);