Basic Algorithm Scripting Flashcards
Cards, resources and exercises of Basic Algorithm Scripting
Creates a function which convert celsius to fahrenheit.
Possible solution
function convertToF(celsius) { let fahrenheit; fahrenheit = celsius * 9/5 + 32; return fahrenheit; }
console.log(convertToF(30));
Reverse a String
Reverse the provided string.
You may need to turn the string into an array before you can reverse it.
Your result must be a string.
Possible solution:
function reverseString(str) { let arrStr = str.split(''); let rvStr = ''; for(let i = arrStr.length - 1; i >= 0; i--){ rvStr += arrStr[i]; } return rvStr ; }
console.log(reverseString(“hello”));
Creates a function witch receives a number and return a factorialize number.
For example: 5! = 1 * 2 * 3 * 4 * 5 = 120
Possible Solution
function factorialize(num) { if (num === 0) { return 1; } return num * factorialize(num-1); }
console.log(factorialize(5));
Find the Longest Word in a String
Possible Solution
function findLongestWordLength(str) { let arrStr = str.split(' '); let longest= '';
for(let i = 0; i < arrStr.length; i++){ if(arrStr[i].length > longest.length) longest = arrStr[i]; } return longest.length; }
console.log(findLongestWordLength(“The quick brown fox jumped over the lazy dog”));
Return Largest Numbers in Arrays
Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.
Possible solution
function largestOfFour(arr) { // You can do this! let arrLargests = []; for(let i = 0; i < arr.length; i++){ arrLargests.push(Math.max(...arr[i])); }
return arrLargests;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Confirm the Ending
Check if a string (first argument, str) ends with the given target string (second argument, target).
This challenge can be solved with the .endsWith() method, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.
Possible Solution
function confirmEnding(str, target) {
return str.slice(str.length - target.length) === target;
}
confirmEnding(“Bastian”, “n”);
Repeat a String Repeat a String
Repeat a given string str (first argument) for num times (second argument). Return an empty string if num is not a positive number.
The built-in repeat()-method should not be used.
Possible Solution
function repeatStringNumTimes(str, num) { if(num <= 0){ return ""}; return str += repeatStringNumTimes(str, num -1); }
repeatStringNumTimes(“abc”, 3);
Truncate a String
Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a … ending.
Possible Solution
function truncateString(str, num) { if(num >= str.length){ return str } return str.slice(0, num) + '...'; }
truncateString(“A-tisket a-tasket A green and yellow basket”, 8);
Finders Keepers
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). If no element passes the test, return undefined.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Possible Solution
function findElement(arr, func) { let num; for(let i = 0; i < arr.length; i++){ num = arr[i]; if(func(num)) return num; } return undefined; }
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.
Possible Solution
function booWho(bool) { let isBool = true; return typeof bool === typeof isBool; }
booWho(null);
Title Case a Sentence
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”.
hint: try to use regex to solve it.
Possible Solution
function titleCase(str) { let regEx = /(^|\s)\S/g; return str.toLowerCase().replace(regEx, (s) => s.toUpperCase()); }
console.log(titleCase(“HERE IS MY HANDLE HERE IS MY SPOUT”));
Slice and Splice
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.
Possible Solution
function frankenSplice(arr1, arr2, n) { let arrSorted = []; arr2.splice(n, 0, ...arr1); arrSorted = [...arr2]; arr2.splice(n, arr1.length); return arrSorted; }
frankenSplice([1, 2, 3], [4, 5, 6], 1);
Falsy Bouncer
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) { return arr.filter(Boolean); }
console.log(bouncer([7, “ate”, “”, false, 9]));
Where do I Belong
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).
Possible Solution
function getIndexToIns(arr, num) {
arr. push(num);
arr. sort((a, b) => a-b)
return arr.indexOf(num);
}
console.log(getIndexToIns([10, 20, 30, 40, 100], 50));
Mutations
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”.
Possible Solution
function mutation(arr) { return arr[1].toLowerCase() .split('') .map((l) => { return arr[0].toLowerCase().indexOf(l) != -1 }) .reduce((p, c) => { return !p || !c ? false : true}); }
console.log(mutation([“hello”, “hey”]));