[JS Practice] Multiplication table

Title :

Multiplication table

Description:

Your task, is to create NxN multiplication table, of size provided in parameter.

for example, when given size is 3:

1 2 3
2 4 6
3 6 9

for given example, the return value should be: [[1,2,3],[2,4,6],[3,6,9]]

Continue reading

[JS Practice] Reverse words(反转字符)

Title :

Reverse words(反转字符)

Description:

Write a reverseWords function that accepts a string a parameter, and reverses each word in the string.

Example:

reverseWords("This is an example!"); // returns  "sihT si na !elpmaxe"

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

[JS Practice] Remove odd number in an array (去掉数组中的奇数)

Title :

Remove odd number in an array (去掉数组中的奇数)

Description:

Write a small function that returns the values of an array that are not odd.

All values in the array will be integers. Return the good values in the order they are given.

要求实现一个方法,在给方法传入一个只包含数字的数组时,方法可以返回数组中以偶数组成的另外一个数组。

Continue reading

[JS Practice] Calculating sum of values in array with reduce(使用reduce方法计算数组元素之和)

Title :

Calculating sum of values in array with reduce(使用reduce方法计算数组元素之和)

Description:

Make the sum() function return the sum of the values in the passed in array. Your solution must use the reduce() function of the built-in javascript Array object. If you’re not familiar with reduce(), reading over the documentation may help.

function sum(array) {
  // Use array.reduce() to find and return the
  // sum of the values in array.
}

For example:

var someNumbers = [1,2,3,4,5,6,7,8,9,10];

sum(someNumbers); // should return 55

Continue reading

[JS Practice] Summing a number’s digits(对各个位数上的数字求和)

Title :

Summing a number’s digits(对各个位数上的数字求和)

Description:

Write a function named sumDigits which takes a number as input and returns the sum of the absolute value of each of the number’s decimal digits. For example:

  sumDigits(10); // Returns 1
  sumDigits(99); // Returns 18
  sumDigits(-32); // Returns 5

Let’s assume that all numbers in the input will be integer values.

Continue reading

[JS Practice] Number-like counter(返回数字类型的counter类)

Title :
Number-like counter(返回数字类型的counter类)

Description:
You’re going to implement a simple counter class. The counter will start at zero, and every time its incr method is called, it will increase by 1.

There’s one caveat: Your counter must act like a number and support arithmetic operations and comparisons.

要求创建一个counter类,和它的一个成员方法incr,当counter的实例每次调用incr时,它的值都会加1.

For example:

var c = new Counter();
c.incr(); // counter is now at 1
c + 1; // 2
c &gt; 1; // false
c &gt; 0; // true
c == 1; // true
Math.sqrt(c); // 1

You are not required to support equality comparison between two Counter instances.

Continue reading