[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] Christmas day

Title :

Christmas day

Description:

Sometimes it’s useful to know on which day of week the holly holiday will occur.
But just don’t limit ourselves to this year and write a function which consumes a Christmas date and outputs a name of weekday.

Example

findOutChristmasWeekday('2013 12 25') // returns 'Wednesday'

Only valid Christmas dates will be passed to the function.

Date parameter could be a string or a Date object. If it’s a string here are possible date parameter formats:

'2013 12 25'
'12 25 2013'
'25 December 2013'

Note: calendar used in the kata is Gregorian.

Continue reading

[JS Practice] Sort characters

Title :

Sort characters

Description:

Write a function called sortGiftCode (sort_gift_code in Ruby) that accepts a string containing up to 26 unique alphabetical characters, and returns a string containing the same characters in alphabetical order.

Examples:

sortGiftCode( 'abcdef' );//=> returns 'abcdef'
sortGiftCode( 'pqksuvy' );//=> returns 'kpqsuvy'
sortGiftCode( 'zyxwvutsrqponmlkjihgfedcba' );
//=> returns 'abcdefghijklmnopqrstuvwxyz'

Continue reading

[JS Practice] Boiled Eggs

Title :

Multiplication table

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

Implement a function, which takes a non-negative integer, representing the number of eggs to boil. It must return the time in minutes (integer), which it takes to have all the eggs boiled.

Rules

  • you can put at most 8 eggs into the pot at once
  • it takes 5 minutes to boil an egg
  • we assume, that the water is boiling all the time (no time to heat up)
  • for simplicity we also don’t consider the time it takes to put eggs into the pot or get them out of it

Example

cookingTime(0); // must return 0
cookingTime(5); // must return 5
cookingTime(10); // must return 10
cookingTime(16); // must return 10

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] 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 > 1; // false
c > 0; // true
c == 1; // true
Math.sqrt(c); // 1

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

Continue reading