[JS Practice] arithmetic list

Title :

Arithmetic list

Description:

Function accept three parameters :

  • first the first term in the sequence
  • c the constant that you are going to add ( since it is an arithmetic sequence…)
  • l the number of terms that should be returned

Sequence in wikipedia: Sequence

Example

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

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] Find majuscule letters

Title :

Find majuscule letters

Description:

Instructions

Write a function capitals that takes a single string (word) as argument. The functions must return an ordered list containing the indexes of all capital letters in the string.

Example

Test.assertSimilar( capitals('CodEWaRs'), [0,3,4,6] );

Continue reading

[JS Practice] Difference of 2 (相差2)

Title :

Difference of 2

Description:

The objective is to obtain the various sets of integers that have a difference of 2 from a given array of integers. The result array should be sorted in ascending order of values.

So for example for the array of integers

[1,2,3,4] should return [[1,3],[2,4]]

[4,1,2,3] should also return [[1,3],[2,4]]

[1,23,3,4,7] should return [[1,3]]

Assume there are no duplicate integers in the array. The order of the integers in the input array should not matter.

Continue reading

[JS Practice] Changing Vowel(替换单词中的元音字母)

Title :

Changing Vowel(替换单词中的元音字母)

Description:

要求实现两个方法,第一个方法在接收到一个包含元音的英文单词后会把元音替换成相应的数字,第二个方法则反过来,会把相应的数字替换成对应的元音单词。
Step 1: Create a function called encode() to replace all the lowercase vowels in a given string with numbers according to the following pattern:

a -> 1

e -> 2

i -> 3

o -> 4

u -> 5

For example, encode(“hello”) would return “h2ll4” There is no need to worry about uppercase vowels in this kata.

Step 2: Now create a function called decode() to turn the numbers back into vowels according to the same pattern shown above.

For example, decode(“h3 th2r2”) would return “hi there”

For the sake of simplicity, you can assume that any numbers passed into the function will correspond to vowels.

Continue reading

[JS Practice] Name Array Capping (第一个字母大写)

Title :

Name Array Capping (第一个字母大写)

Description:

Create a method that accepts an array of names, and returns an array of each name with its first letter capitalized.

完成一个方法,实现当方法在接收到一个以字符串为元素的数组时,方法能否使每个元素的第一个字符大写。

example :

capMe([<span class="string">'jo'</span>, <span class="string">'nelson'</span>, <span class="string">'jurie'</span>]) <span class="comment">// returns ['Jo', 'Nelson', 'Jurie']</span> 
capMe([<span class="string">'KARLY'</span>, <span class="string">'DANIEL'</span>, <span class="string">'KELSEY'</span>]) <span class="comment">// returns ['Karly', 'Daniel', 'Kelsey']</span>

Continue reading