Js basics of pit removal diaries (ECMA262)

preface

Although I am a beginner, I want to be a good front end.

I’m writing this to learn better, grow faster, get out of the hole early, Emmmm.

Basically, each article solves a problem in the content I am learning and combs the knowledge points.

Due to the limited level, it is hard to avoid mistakes, welcome correction.

The current problem

Implements a function to determine whether the input is a palindrome string.

Analysis of the

  • Use the function+if statement
  • Determine if it is a string using the typeof operator
  • To determine whether palindromes use string method and object method, split the string with split method, reverse the order with object method, and finally join the string with object method

knowledge

Variable types and calculations

Value type and reference type

  1. ECMAScript has five simple data types, Undefined, Null, Boolean, Number, and String, as well as a complex type, Object.
  2. A value type is stored in a stack, which is a simple piece of data that overlaps a variable (a variable is just a placeholder for a value in the stack), that is, where the variable is accessed.
  3. Reference types are stored in the heap, which is a dynamic area of memory, equivalent to reserved space, that is dynamically allocated to code and stack during program execution.
  4. Equal value types create a new data segment each time, and equal reference types refer to the same data block.
var a={name:"Xiao Ming"}
var b=a
b.name="Little red"The console. The log (a.n ame) / / little redCopy the code

The typeof operator

typeof undefined //undefined
typeof 'abc' //string
typeof  1 // number
typeof  false //boolean
typeof  null //object
typeof  {} //object
typeof  [] //object
typeof  console.log //object
Copy the code
  1. As you can see from the above code, type can only detail value types, not reference types.
  2. Null is also a value type, but from a logical point of view it is a null object pointer.

Variable calculation == and ===

100 ='100'//true0 =' '//true
null=undefined//true
Copy the code

=== is not strictly equal, === is strictly equal. Implicit type conversions occur, so use them with caution. This can only be used when the following conditions occur

if(obj. A = = null) {obj. At this time a = = null is equivalent to obj. A = = = null | | obj. A = = = the abbreviated form of undefined}Copy the code

Split, join, reverse

join

  1. Definition: The join() method is used to put all the elements of an array into a string. Elements are separated by the specified delimiter.
  2. The optional argument 1 is used to specify the delimiter. If this parameter is omitted, a comma is used as a delimiter.
var arr=["M"."a"."r"."c"."u"."s"]
arr.join("--")//M--a--r--c--u--s
Copy the code

split

  1. Definition: The split() method splits a string into an array of strings.
  2. The argument 1, string or regular expression must be split from the point specified by the argument. Optional argument 2, which specifies the maximum length of the array to return. If this parameter is set, no more substrings are returned than the array specified by this parameter. If this parameter is not set, the entire string is split regardless of its length.
var str="How are you doing today?"

document.write(str.split("") + "<br />")//How,are,you,doing,today? // If an empty string ("") is used as separator, then each character in stringObject is separated. document.write(str.split("") + "<br />")//H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,? // If an empty string ("") as separator, stringObject is split into words. document.write(str.split("",3))//How,are,you

Copy the code

reverse

  1. Definition: Reverse is used to reverse the order in an array
var a=["1"."2"."3"]
a.reverse()// ["3"."2"."1"]
Copy the code
  1. This method changes the original array rather than creating a new one.

The answer


function run (input){
    if(typeof input! = = ="string") {return false
    }
    else{
        if (input.split(' ').reverse().join(' ')===input){
            return true}}}Copy the code

An if statement can omit the method body if it is a single sentence, and else statements can return the result directly.

function run(input) {
  if(typeof input ! = ='string') return false;
  return input.split(' ').reverse().join(' ') === input;
}
Copy the code