[JS Practice] like function(类似Facebook上的一个点赞方法)

Title :
Like function(类似Facebook上的一个点赞方法)

Description:
You probably know the “like” system from Facebook and other pages. People can “like” blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.

Implement a function likes(), which must take in input array, containing the names of people who like an item. It must return the display text as shown in the examples:

likes([]); // must return “no one likes this”
likes([‘Peter’]); // must return “Peter likes this”
likes([‘Jacob’, ‘Alex’]); // must return “Jacob and Alex like this”
likes([‘Max’, ‘John’, ‘Mark’]); // must return “Max, John and Mark like this”
likes([‘Alex’, ‘Jacob’, ‘Mark’, ‘Max’]); // must return “Alex, Jacob and 2 others like this”
For more than 4 names, the number in and 2 others simply increases.

要求实现一个likes方法,输入的是一个包含所有点赞的用户名的数组,输出的是包含这些用户名的一句text,如果用户数量小于等于三个,则全部显示在句子里,如果超过,则显示显示前两个,剩下的显示剩余用户数量。考察到javascript的基本输出语法。
Continue reading

[JS practice] Square Sum (平方求和)

Title :
Square Sum (对数组中每个元素求平方,然后求和)

Description :
Complete the squareSum method so that it squares each number passed into it and then sums the results together.

要求对输入数组里所有数字先求平方,然后再对得出的结果求和。
虽然不难,但是会考察到javascript里两个基本函数和/或一个基本对象的使用。

For example:
squareSum([1,2, 2]); // should return 9

Continue reading