JavaScript Practice notes Finishing 1-6.23

Source: Internet
Author: User
Tags pow

Practice Platform Codewars Address: https://www.codewars.com/Welcome to discuss ~╭ (???) with everyone? ?? Basic Exercises (1):

My answer is:

class Smallestintegerfinder {  findsmallestint (args) {    var a = Args[0];      for (var i=0; i<args.length; i++) {      if(Args[i] <= a) {        = args [i];      }    }     return A;  }}

The better answer is:

class Smallestintegerfinder {  findsmallestint (args) {    return Math.min.apply (null  , args);}  }

Analysis:

It can be seen that the method I'm thinking of is still in a relatively rudimentary phase, and I didn't think of using the Math object method. Use the method to reduce the amount of code, more concise. It also shows that the content of this part of common methods is not proficient.

Notes:

The Math.min () method and the Math.max () method are used to determine the minimum and maximum values in a set of values. You can receive any number of numeric parameters. apply () methodis to call a function in a specific scope, which is equivalent to setting the value of Thisui in the function body. The Apply () method receives two parameters: one is the scope in which the function is run, and the other is the parameter array. A similar alternative to the call () method, which has the same effect as the Apply () method, differs in how the parameters are received. The first parameter received by the call () method is also this value, and the remaining parameters are passed directly to the function, and the arguments passed to the function must be enumerated individually. The Apply () method and the call () method extend the scope of the function run, and the object does not need to have any coupling to the method.The two methods in the elevation use the example to derive the most value:var values = [1,2,3,4,5,6,7,8];var max = Math.max.apply (math,values);Math.min (... args) new features in JavaScript, extended syntax (spread syntax), precede the object with ... , you can traverse the object. For more information, refer to: Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator

Basic Exercises (2)

My answer is:

function Removesmallest (Numbers) {  var lowest = Math.min.apply (math,numbers);   var index = numbers.indexof (lowest);  Numbers.splice (Index,1);   return numbers;}

The better answer is:

function Removesmallest (Numbers) {  if(!numbers)return  [];   var min=math.min.apply (null, numbers);  Numbers.splice (Numbers.indexof (min),1);   return numbers;}

Analysis:

The idea is to use the Math.min.apply () method to find the minimum value, and then use the IndexOf () method to find the minimum value in the array index value, and finally through the splice () method, by setting the index value and the number of deleted items to delete the minimum value. The difference is that before performing the operation of these columns, the array is judged to be empty, and if empty, an empty array is returned.

Notes:

indexOf () methodReturns the position of the item to find in the array, or 1 if not found, receiving two parameters: the item to find and an index that represents the location of the lookup start (optional). The IndexOf () method represents a backward lookup starting at the beginning of the array, while the LastIndexOf () method looks forward from the end of the array. Splice () methodThe main purpose is to insert items in the middle of the array, but there are three ways to use the method, namely delete, insert, and replace. The splice () method always returns an array containing the items removed from the original array, and an empty array if no items are deleted. The method will change the original array. Three ways of using this are as follows: Delete: You can delete any number of values, specify two parameters: the position of the first item to delete, and the number of items to delete. Insert: You can insert any number of items to a specified location, you need to provide three parameters: the starting position, the number of items to delete (typically 0), and the item to insert. Replace: You can insert any number of items at the specified location and delete any number of items at the same time, specifying three parameters: The starting position, the number of items to delete, and any number of items to insert.

Basic Exercises (3)

My answer is:

function squaredigits (num) {  var arr = num.tostring (). Split ("");   var newarr = Arr.map (function(item,index,array) {    return Math.pow (item,2 );  });   var newnum = number (Newarr.join (""));   return Newnum;}

The better answer is:

function squaredigits (num) {  return number ((' + num). Split ("). Map (functionreturn Val * val;}). Join ('));}

Analysis:

In contrast, better solutions generally prefer to use a single statement to derive an answer, and the methods used are similar. When the numeric data is converted to character data, he uses a weak-type conversion. Then in the calculation of the square, the use of the method is directly multiplied, I think it is not conducive to future code modification, such as the change to any of the second party? , so I think it would be better to use the Math.pow () method.

Notes:

Split () methodYou can split a string into multiple substrings based on the specified delimiter and place the result in an array. The delimiter can be a string or a RegExp object. You can receive the optional second parameter, which specifies the size of the array, to ensure that the returned array does not exceed the specified size. map () methodis the array in which each item in the array runs the given function, returning the result of each function call, and each item of the arrays is the result of running the incoming function on the corresponding item in the original array. Similar iterative methods include every (), filter (), ForEach (), and some (). The Math.pow () method, which receives two parameters: num and power, returns the power of Num. Join () methodYou can use a different delimiter to build a string, receive only one argument, use it as a delimiter string, and then return a string containing all the array direction. If you do not pass any value to the join () method or pass in undefined, use a comma as the delimiter. today's summary:before also practiced in the Freecodecamp, and Codewars different is the Freecodecamp comparative basis, do exercise when there are corresponding method hints, do not need to think can be based on the method to test, So just know the existence of these methods and in the use of the time it is difficult to think of oh the original can be used or this method is specifically how to use. And in Codewars is only the topic, rely on their own thinking solution, and the answer can see other people's better answers. In order to answer the process of the problem, book online to find information, the last 1.1 points from the wrong to the right, or quite a sense of accomplishment. It is also better to know how to answer by comparing yourself with the answers. Finally, the method is summed up again, you can remember.

JavaScript Practice notes Finishing 1-6.23

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.