-
Notifications
You must be signed in to change notification settings - Fork 0
Intermediate Algorithm Scripting Challenges
We'll pass you an array of two numbers. Return the sum of those two numbers plus the sum of all the numbers between them.
The lowest number will not always come first.
function sumAll(arr) {
function range(a, b) {
return Array(Math.max(a, b) - Math.min(a, b) + 1).fill().map((x, i) => Math.min(a, b) + i)
}
return range(arr[0], arr[1]).reduce((a, b) => a + b, 0);
}
sumAll([1, 4]);
Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Note You can return the array with its elements in any order.
function diffArray(arr1, arr2) {
return [...(arr1.filter(x => !arr2.includes(x))), ...(arr2.filter(x => !arr1.includes(x)))];
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.
Note You have to use the arguments object.
function destroyer(arr) {
const args = Array.from(arguments).slice(1);
return arr.filter(el => !args.includes(el));
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching name and value pairs (second argument). Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.
For example, if the first argument is [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }]
, and the second argument is { last: "Capulet" }
, then you must return the third object from the array (the first argument), because it contains the name and its value, that was passed on as the second argument.
function whatIsInAName(collection, source) {
const sourceKeys = Object.keys(source);
return collection.filter(obj => {
return sourceKeys.map(key => {
return obj.hasOwnProperty(key) && obj[key] === source[key];
}).reduce((a, b) => a && b);
})
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
return str.split(/\s|_|(?=[A-Z])/).join('-').toLowerCase();
}
spinalCase('This Is Spinal Tap');
Translate the provided string to pig latin.
Pig Latin
takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an "ay".
If a word begins with a vowel you just add "way" to the end.
Input strings are guaranteed to be English words in all lowercase.
function translatePigLatin(str) {
// Create variables to be used
var pigLatin = '';
var regex = /[aeiou]/gi;
// Check if the first character is a vowel
if (str[0].match(regex)) {
pigLatin = str + 'way';
} else if(str.match(regex) === null) {
// Check if the string contains only consonants
pigLatin = str + 'ay';
} else {
// Find how many consonants before the first vowel.
var vowelIndice = str.indexOf(str.match(regex)[0]);
// Take the string from the first vowel to the last char
// then add the consonants that were previously omitted and add the ending.
pigLatin = str.substr(vowelIndice) + str.substr(0, vowelIndice) + 'ay';
}
return pigLatin;
}
translatePigLatin("consonant");
Perform a search and replace on the sentence using the arguments provided and return the new sentence.
First argument is the sentence to perform the search and replace on.
Second argument is the word that you will be replacing (before).
Third argument is what you will be replacing the second argument with (after).
Note Preserve the case of the first character in the original word when you are replacing it. For example if you mean to replace the word "Book" with the word "dog", it should be replaced as "Dog"
function myReplace(str, before, after) {
const index = str.indexOf(before);
if (str[index] === str[index].toUpperCase()) {
after = after.charAt(0).toUpperCase() + after.slice(1);
}
str = str.replace(before, after);
return str;
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array.
Base pairs are a pair of AT and CG. Match the missing element to the provided character.
Return the provided character as the first element in each array.
For example, for the input GCG, return [["G", "C"], ["C","G"],["G", "C"]]
The character and its pair are paired up in an array, and all the arrays are grouped into one encapsulating array.
function pairElement(str) {
const pairs = {A:'T',T:'A',G:'C',C:'G'};
return str.split('').map(c => [c, pairs[c]]);
}
pairElement("GCG");
Find the missing letter in the passed letter range and return it.
If all letters are present in the range, return undefined.
function fearNotLetter(str) {
const letters = 'abcdefghijklmnopqrstuvwxyz';
const slice = letters.substr(letters.indexOf(str[0]), str.length+1);
for (let i = 0; i < slice.length; i++) {
if(slice[i] !== str[i]) return slice[i];
}
return undefined;
}
fearNotLetter("abce");
Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays.
In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.
Check the assertion tests for examples.
function uniteUnique(...arr) {
return [...new Set([].concat(...arr))];
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
Convert the characters &, <, >, " (double quote), and ' (apostrophe), in a string to their corresponding HTML entities.
function convertHTML(str) {
const entities = {
'&':'&',
'<':'<',
'>':'>',
'"':'"',
"'":'''
};
return str.split('').map(c => {
if(c in entities) return entities[c];
return c
}).join('');
}
convertHTML("Dolce & Gabbana");
Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num.
The first two numbers in the Fibonacci sequence are 1 and 1. Every additional number in the sequence is the sum of the two previous numbers. The first six numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8.
For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less than or equal to 10 are 1, 1, 3, and 5.
function sumFibs(num) {
if (num <= 0) return 0;
const repo = [1, 1];
let next = 0;
while (repo[0] + repo[1] <= num) {
next = repo[0] + repo[1];
repo.unshift(next);
}
return repo.filter(x => x % 2 !== 0).reduce((a, b) => a + b);
}
sumFibs(4);