JavaScript Practice notes Finishing 2-6.24

Source: Internet
Author: User

Codewars Address: https://www.codewars.com/Welcome to discuss with you all Basic Exercises (1):My answer is:
function Isisogram (str) {  ifreturntrue;   return str.tolowercase (). Split (""). Sort (). Every (function(item,index,array) {    return Item! = array[index+1];  });; }
Better Solution One:
function Isisogram (str) {  return !/(\w). *\1/i.test (str)}

Better solution Two:

function Isisogram (str) {  var  I, J;   = str.tolowercase ();    for (i = 0; i < str.length; + +i    )  for (j = i + 1; j < Str.length; + +j      ) if (Str[i] = = = str[j        ]) return false ;   return true;
Analysis:

The first small partner is to use regular expressions to determine whether there are duplicate values, the method is simple, and I did not think in the beginning can use this method, Hhhh. The second small partner is first to lowercase, and then a composite loop to compare each string to a string other than itself, and return False if it is equal. And my train of thought is similar to the second one, which first determines whether it is an empty string, or True if it is empty. Then turn to lowercase, use a space-delimited string into an array, sort it using an iterative method, determine if the same character is present in the sorted array, and return true if it does not exist.

When the iteration method was first used, it was not considered that each item was compared with each item in the array, and itself would return false, resulting in an error. After checking the data, we chose to sort the first and then compare each item with the subsequent one to determine if there is a duplicate value. By contrast, I think it's a bit more complicated.

notes:

Regular expressions: Regular expressions are supported by the RegExp type, and the syntax for creating regular expressions is:var expression =/pattern/flags;

Where pattern type is any regular expression. Each regular expression can have one or more flags flags that indicate the behavior of the regular expression. The matching pattern of regular expressions supports three flags:g (Global), I (not distinguished from large messages), M (multiline mode). Because the regular expression is more complex, the specific pattern is no longer described here.

Look at the regular expression of the better Jie Yi/(\w). *\1/i, which is explained as follows: parentheses () indicate that the matched string is extracted and the characters are grouped. The \w in parentheses indicate that any word character that includes an underscore is matched. Point. Represents any character other than a match line break. An asterisk * indicates a match 0 or more times. The \1 represents the same content that is to be matched in the first parenthesis. Note \1 must be used with parentheses.

The Test () method after the regular expression can tell if the target string matches a pattern and receives a string argument. Returns true if the pattern matches the parameter. Otherwise, returns false.

Basic Exercises (2):

My answer:

function digitize (n) {  return n.tostring (). Split (""). reverse ();

When I first saw the title, I was full of confidence, wow, it's simple. First, convert the numeric data to a character type, then use the split () method to convert to an array, and finally use the reverse () method to flip the array. Results... There was a ... Such a result ...

Think of the solution, doubt is not the problem of the test questions, and finally ran to see the answer, only to know the need to change into an array after the character to the numeric type, in order to avoid the above error.

function digitize (n) {  return n.tostring (). Split (""). Map (number). reverse ();}

Basic Exercises (3):

My answer is:

var function (n) {  ifreturnfalse;   if (MATH.SQRT (n)% 1 = = 0) {returntrue}   Else {returnfalse};}

The better solution is:

function issquare (n) {  return math.sqrt (n)% 1 = = = 0;}
Analysis:

In contrast, the idea is the same, and my answer is to write a few more statements, not concise, the problem is very simple, all without notes, Hhhh.

Basic Exercises (4):

My answer is:

function Getsum (A, b  ) {ifreturn  A;   var i = a>b? b:a;   var length = (a>b?a:b) +1;   var sum = 0;    for (var i;i<length;i++) {    var sum = sum + i;  }   return sum;}

The better solution is:

Const GETSUM = (A, b) + =  {= math.min (a      , b), = Math.max (A, b)  ; return (max-min + 1) * (min + max)/2 ;}
Analysis:

My main idea is to find out the maximum and minimum values of a, B and then use for the loop to find the number between them, the idea is very wonderful, Hhhh. And the better solution of this small partner to find the most value later, with the arithmetic progression formula, really witty approach!!! Really want to learn programming also need to be able to use good mathematics knowledge, learned, Hhhh.

Summary:

The basic knowledge of today's topic is the same as that of yesterday, and it is becoming more and more skillful in using methods. However, when doing the topic, often encounter some of their own to take it for granted that the solution, the results of a test will find that there is a mistake, the feeling is somehow unable to find the wrong time, often fried hair. Today is the second practice of the mistake, in the past is the case, this time dug the pit, also know that later encountered problems will change the thinking, to solve the problem, not in the doubt of life. The second time is not a good understanding of the topic, thought is very simple, the result is their own understanding of the error, this is probably also to understand the meaning of user demand, Hhhh.

Accidentally did an upgrade of the topic, a smooth upgrade, see Codewars tips is full of encouragement, is really a long way away, I will go up and down and quest Ah, continue to refuel tomorrow!

 

JavaScript Practice notes Finishing 2-6.24

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.