JavaScript Practice notes Finishing 3-6.25

Source: Internet
Author: User

Welcome to discuss with everyone Basic Exercises (1):

my answer is:
 function   Array_diff (A, b) { if  (b = = "") return   a  ;  return  a.filter (function   (Item,index,array) { var  flag = false  ;  for  (var  i=0;i<b.length;i++< Span style= "color: #000000") { if  (item!== b[i]) flag = true  ;   return   flag; });}

The better answer is:

function Array_diff (A, b) {  return a.filter (functionreturnb.indexof (x) = =-1 ; });}

Analysis:

Array A uses the filter () method, which iterates over whether the value in the arrays a exists in array B. When the value of x, that is, the value of array a cannot be found in array B, the B.indexof () method returns-1. The filter () method of an array is an array of items that run the given function for each item in a set, returning the function that returns TRUE. My thinking is a little more complex, the method of iterating the array filter () is thought of, but not well utilized, the function of the judgment method is not concise, and did not think that can use the indexof () method to judge.  

Notes:

The filter () method , which refers to an array of items that run a given function, returns the true of each item in a group. Determines whether an item is included in the returned array, using the specified function. Examples of use:var numbers = [1,2,3,4,5,4,3,2,1];var filterresult = numbers.filter (function (item,index,array) {return (ITEM>2);});Alter (FILTERRESULT);//[3,4,5,4,3]an iterative method of an array is really common and is used to loop an operation on an array. These iterative methods are much simpler than for loop statements, remember! There are five iterative methods: every (), filter (), ForEach (), map (), some (). Basic Exercises (2):my answer is:
varGimme =function(Inputarray) {varNewArray = [];  for(vari=0;i<inputarray.length;i++) {Newarray[i]=Inputarray[i]; } newarray.sort (function(A, b) {if(A <b) {return-1; } Else if(a>b) {return1; } Else {        return0;  }  }); returnInputarray.indexof (newarray[1]);};

The better answer is:

function Gimme (a) {  return a.indexof (A.concat (). Sort (functionreturn -A-B}) [1]} 

Analysis:

In the better solution, the concat () method is used for the original array, which copies the original array and creates a new array. The new array is then sorted, and the index values are evaluated for the intermediate values. my thinking and the best solution is the same, and the implementation of the method is still a bit immature. It is not known that creating a new array can be quickly replicated using the Concat () method, and it also indicates that familiarity with the basics is insufficient. In addition to the sorting method, the original can be directly used "return a-B", my method seems very cumbersome.  

Notes:

the concat () method creates a new array based on all the items in the current array. The method first creates a copy of the current array, adds the received parameters to the end of the copy, and returns the newly constructed array. In the absence of a parameter passed to the Concat () method, it is simply copied. If one or more arrays are passed to the concat () method, the method adds each item in the array to the result array. If the passed value is not an array, it is added to the end of the result array. Examples of use:var colors = ["Red", "green", "Blue"];var colors = Colors.concat ("Yellow", ["Black", "Brown"]);alert (colors);//Red,green,bluealert (colors);//Red,green,blue,yellow,black,brown  Reorder Methods:you can sort by using the sort () method, but there may still be problems, so you can avoid this problem by using a comparison function. for most data types to use, simply pass them as parameters to the sort () method:function Compare (value1,value2) {if (value1 < value2) {return-1;} else if (value1> value2) {return 1;} else {Returm 0;     }} for numeric or other valueof () methods that return an object type of numeric type, you can use a simpler comparison function:function Compare (value1,value2) {return value2-value1;} Basic Exercises (3):

my answer is:
function Minmax (arr) {  var newarr = [];  Newarr.push (Math.min.apply (Math,arr));  Newarr.push (Math.max.apply (Math,arr));   return Newarr;}

The better answer is:

function Minmax (arr) {  return  [Math.min (... arr), Math.max (... arr.)];}

Analysis:

The problem is simple, and the extended syntax (spread syntax) in the better answer is also mentioned in practice one. I am still too cautious in writing, should I be bold? Basic Exercises (4):

my answer is:
functionXO (str) {varstr =str.tolowercase (); varCountx = 0; varCounto = 0;  for(vari=0;i<str.length;i++) {     if(Str[i] = = = "X") {Countx++; }     if(Str[i] = = = "O") {Counto++; }    }    if(Counto = =Countx) {      return true; } Else {      return false; }}

The better answer is:

function XO (str) {    var a = str.replace (/x/gi, '),          = str.replace (/o/gi, ');     return a.length = = = b.length;}

Analysis:

the better solution uses the Replace () method, combined with the use of the regular expression, the original string str respectively x and O with an empty string replaced by A and B strings, compare the length of a and B strings, resulting in the results. My solution, because I really can not imagine what method to use, so the most primitive method, as if they are doing the C language topic.  notes: Replace () method , which accepts two parameters, a parameter can be a RegExp object or a string, the second argument can be a string or a function. If the first argument is a string, the command replaces the first substring. To replace all substrings, provide a regular expression and specify the global flag. Examples of use:var text = "Cat,bat,sat,fat";var result = Text.replace ("At", "ond");alert (result);//"Cond,bat,sat,fat" result = Text.replace (/at/g, "ond");alert (result);//"Cond,bond,song,fond" Summary:today's knowledge is primarily a filter () method in the array's iterative approach, the concat () method in the array manipulation method, and the replace () method of the string. The filter () method can be used to determine the array of values in the array that return a true value from the value of the items in the arrays. Concat () can copy and create new arrays. The replace () method can replace one or more values in a string. Judging from these three-day exercises, the various methods for arrays are becoming more and more skillful. But other types of methods are still a challenge. And my solution is to write more concise and effective statements from the more redundant statements. Come on, let's go!

JavaScript Practice notes Finishing 3-6.25

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.