A few little exercises on the JavaScript algorithm--donyz

Source: Internet
Author: User
Tags first string

Dear friends, the following content is from my practice on the Freecodecamp, this is a good learning platform, here to share to everyone, the following question answer in the end of the article (the answer is written by myself, although all written, but it is not necessarily the best way to write, So hope to communicate with you, hey, and will continue to update the article on the JavaScript algorithm, I hope that the small partners can communicate more, learn from each other

1. Flipping a string

First, convert the string into an array, then use the reverse method of the array to flip the order of the arrays, and finally convert the arrays into strings.

Your result must be a string.

For example: reversestring ("Hello");

Olleh

2. Calculate the factorial of an integer

If the letter n is used to represent an integer, the factorial represents the product of all integers less than or equal to N.

Factorial is usually abbreviated as n!.

Example: 5! = 1 * 2 * 3 * 4 * 5 = 120

Factorialize (5);
120

3. Returns true if the given string is a palindrome, and returns false instead.

If a string ignores punctuation, capitalization, and whitespace, it is exactly the same as reading and reading back, then the string is Palindrome (palindrome).

Note that you need to remove the extra punctuation and whitespace from the string, and then convert the string to lowercase to verify that the string is a palindrome.

The value of the function parameter can be "racecar", "racecar", and "race CAR".

For example: Palindrome ("eye");

True

4. Find the longest word in the provided sentence and calculate its length.

The return value of the function should be a number.

For example: Findlongestword ("The quick brown fox jumped over the lazy dog");

6

5. Make sure that the first letter of each word in the string is capitalized and the remainder is lowercase.

Connectors like ' the ' and ' of ' are the same.

For example: Titlecase ("I ' m a Little tea pot"); I ' m A Little Tea Pot

6. The Large array on the right contains 4 decimal groups, each of which finds the maximum value in each decimal group, and then concatenates them together to form a new array.

Tip: You can use a for loop to iterate through algebraic groups and access each element of the array in a way that arr[i].

For example: Largestoffour ([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]); [5,27,39,1001]


7. Check whether a string (str) ends with the specified string (target).

Returns true if it is, or false if it is not.

For example: confirmending ("Bastian", "n"); True

8. Important thing to say 3 times!

Repeats a specified string num times, and returns an empty string if num is a negative number.

For example: Repeat ("abc", 3); Abcabcabc

9. Use Rez apart to truncate the opposite retreat! Truncate a String!

If the length of the string is longer than the specified parameter num, the extra part is used ... To express.

Remember that the three-point number inserted at the end of a string also accounts for the length of the string.

However, if the specified parameter num is less than or equal to 3, the added three point number is not counted into the length of the string.

For example: Truncate ("Absolutely longer", 2); Ab...


monkeys eat bananas but break into several sections to eat Oh!

Divides an array of arr into a number of array blocks, as specified by the size of the arrays.

For example: Chunk ([1,2,3,4],2) =[[1,2],[3,4]];

Chunk ([1,2,3,4,5],2) =[[1,2],[3,4],[5]];

Chunk ([0, 1, 2, 3, 4, 5], 4); [[0,1,2,3],[4,5]]

11. Can't kill the cockroach!

Returns the remaining elements after an array is truncated by n elements, starting with the truncation from index 0.

For example: Slasher ([1, 2, 3], 2); [3]

12. Toad can eat teammates, also can eat opponent.

The function returns True if the first string element of the array contains all the characters of the second string element.

For example, ["Hello", "hello"] should return true because all the characters of the second string can be found in the first string if the case is ignored.

["Hello", "hey"] should return false because the string "hello" does not contain the character "Y".

["Alien", "line"] should return true, because all characters in "line" can be found in "Alien".

For example: mutation (["Hello", "hello"]); True


13. True and False Monkey King!

Deletes all false values in the array.

In JavaScript, false values are false, NULL, 0, "", Undefined, and NaN.

For example: Bouncer ([False, NULL, 0, NaN, Undefined, ""]); //[]


14. Jinx's Mortar!

Implement a Destroy (destroyer) function, the first parameter is the array to be destroyed, the remaining parameters are the values to be destroyed.

For example: Destroyer ([1, 2, 3, 1, 2, 3], 2, 3); [About]


15. Where is my body?

Sorts the array first, then finds the specified value in the array's position, and finally returns the index of the position.

Example: where ([1,2,3,4], 1.5) should return 1. Since 1.5 is inserted into the array [1,2,3,4] and becomes [1,1.5,2,3,4], the 1.5 corresponding index value is 1.

Similarly, where ([20,3,5], 19) should return 2. Because the array is sorted first [3,5,20],19 into the array [3,5,20] and becomes [3,5,19,20], the 19 corresponding index value is 2.

For example: where ([40, 60], 50); 1

The answer can refer to My github (non-standard, for reference only), like a small partner can star, and then the JavaScript algorithm for the small partners can also pull requests, or directly contact me, hey

A few little exercises on the JavaScript algorithm--donyz

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.