[JS Practice] execute multiple functions successively on a value

Title :

Execute multiple functions successively on a value

Description:

Write a generic function chainer that takes a starting value, and an array of functions to execute on it. Return the final value after execution is complete.

Example

function add(num) { 
   return num + 1 
} 

function mult(num) { 
   return num * 30 
} 

chain(2, [add, mult]); // returns 90;

Continue reading

Advertisements

[JS Practice] Array Appender

Title :

Array Appender

Description:

You are the greatest chef on earth. No one boils eggs like you! Your restaurant is always full of guests, who love your boiled eggs. But when there is a greater order of boiled eggs, you need some time, because you have only one pot for your job. How much time do you need?

Your Task

Write a function called appendArrays that appends the items from array 2 onto array 1, returning the newly formed array.

For example if your 2 arrays were:

var array1 = [a,b,c]
var array2 = [1,2,3]

After using your appendArrays function, the result should be[a,b,c,1,2,3]

Your function should also be able to handle nested arrays.

For example, combining array [['x','x'],'B'] with array['c','D'] should return [['x','x'],'B','c','D'].

Your solution should account for a situation for either the first or second inputs aren’t actually arrays.

// basic test
Test.assertSimilar(appendArrays(['this'],['that']), ['this','that'])

// second input is not an array
Test.assertSimilar(appendArrays([1,2], [1]), [1,2,1])

// first input is not an array
Test.assertSimilar(appendArrays([2], [1,1,1]), [2,1,1,1])

Continue reading

[JS Practice] Sum of two array elements equals to target

Title :

Sum of two array elements equals to target

Description:

Write a function that takes an array of numbers (integers for the tests) and a target number. It should find two different items in the array that, when added together, give the target value. The indices of these items should then be returned in an array like so: [index1, index2].

The input will always be valid (numbers will be an array of length 2 or greater, and all of the items will be numbers; target will always be the sum of two different items from that array).

Example

twosum([1,2,3], 4); // must return [0, 2]

Continue reading

[JS Practice] Adjacent pairs in a string

Title :

Adjacent pairs in a string

Description:

Supplied with a single string, return the number of all adjacent pairs in that string.

The words within the string are separated by whitespace.

The function should be case-insensitive (i.e., the input string “orange OrAnGe” is a match).


Some example input/output:


//returns 0
countAdjacentPairs('') 

// returns 1
countAdjacentPairs('cat dog dog') 

// returns 1 (The first pair has been matched, and will be ignored from then on).
countAdjacentPairs('dog dog dog') 

// returns 2
countAdjacentPairs('cat cat dog dog cat')

Continue reading

[JS Practice] Anagram Detection

Title :

Anagram Detection

Description:

According to wikipedia :

An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; for example Doctor Who can be rearranged into Torchwood.

An anagram is the result of rearranging the letters of a word to produce a new word. (Ref wikipedia).

Note: anagrams are case insensitive

Examples

  • foefet is an anagram of toffee
  • Buckethead is an anagram of DeathCubeK

The challenge is to write the function isAnagram to return true if the word test is an anagram of the word original and false otherwise.

Continue reading