JavaScript Practice notes Finishing 4-6.26

Source: Internet
Author: User
Tags iterable

Basic Exercises (1):

my answer is:
function Getmiddle (s) {  if(s.length%2 = = 0)    {return S.charat (s.length/2-1) + S.charat (S.length/2);   Else {    return S.charat (S.LENGTH/2);  }}

The better answer is:

function Getmiddle (s) {  return s.substr (Math.ceil (S.LENGTH/2-1), s.length% 2 = = 0? 2:1);} 

Analysis:my practice is to first determine the length of the string, depending on the length to select the characters that need to be returned. The substr () method, the Math.ceil () method, and a ternary operator are used in the preferred solution. The substr () method creates a new string, and the Math.ceil () method is used to round a number.  notes:The charAt () method , which is used to access a specific character in a string, returns that character in a given position as a single-character string, receiving an argument, which is based on the 0 character position.  The substr () method , which is based on a method that creates a new string from a string, returns a substring of the manipulated string, receives one or two arguments, the first parameter specifies the starting position of the substring, and the second parameter indicates where the substring ends. The second parameter of substr () specifies the number of characters returned. If a second argument is not passed, the end of the string is used as the ending position. No effect on the original string.

Basic Exercises (2):

my answer is:
var uniqueinorder=function(iterable) {  var newarr = [];    for (var i=0;i<iterable.length;i++) {    if(Iterable[i]!== iterable[i+1]) {      Newarr.push (Iterable[i]);    }  }   return Newarr;}

Better Solution One:

function Uniqueinorder (IT) {  var result = []  var    last for ( var i = 0; i < it.length; i++) {    if (It[i]!== last ) {      = it[i])    }  }     return  result}

Better answer two:

var function (iterable) {  return [].filter.call (iterable, (functionreturn iterable[i-1] !== A});}

Analysis:My idea is to judge whether each bit is equal to the next, and if it is equal, it is added to the new array based on the current string. The better solution is to set a value in the new array as the base of the decision, if the string is not equal to the value in the array, it is added to the new array. PS: Today's state is very poor, the mentality of some more negative emotions, so do the exercise is less, notes are also less. (lazy)

JavaScript Practice notes Finishing 4-6.26

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.