You need to know some of the JavaScript face questions (medium)

Source: Internet
Author: User

9. Implement function Isinteger (x) to determine if X is an integer

You can convert the X to 10, judging if it is equal to Itself:

function Isinteger (x) {return parseint (x, ten) = = = x;}

ES6 extends the value, providing a static method Isinteger () to determine whether the parameter is an integer:

Number.isinteger (+)//truenumber.isinteger (25.0)//truenumber.isinteger (25.1)//falsenumber.isinteger ("15")// Falsenumber.isinteger (true)//false

JavaScript can accurately represent an integer range between -2^53 and 2^53 (with no two endpoints), exceeding this range, and cannot accurately represent this Value. ES6 introduces two constants, Number.max_safe_integer and number.min_safe_integer, to represent the upper and lower bounds of this range, and provides a number.issafeinteger () To determine if an integer is a safe integer.

10. In the following code, in what order does the number 1-4 output? Why is this output?

(function () {console.log (1);     SetTimeout (function () {console.log (2)}, 1000);     SetTimeout (function () {console.log (3)}, 0); Console.log (4);}) ();

This is not much to explain, mainly JavaScript timing mechanism and time loop, do not forget that JavaScript is Single-threaded. The detailed explanation can refer to the JavaScript running mechanism from Settimeout.

11. Write a function less than 80 characters to determine if a string is a palindrome string

function Ispalindrome (str) {str = str.replace (/\w/g, "). tolowercase (); return (str = = Str.split ("). reverse (). join ('));}

This question I met on the codewars, and included some good solution, can poke here: palindrome for Your Dome

12. Write a sum method that will work correctly as called below

Console.log (sum (2,3));  Outputs 5console.log (sum (2) (3)); Outputs 5

For this problem, you can determine the number of parameters to Achieve:

function sum () {var fir = arguments[0]; if (arguments.length = = = 2) {return arguments[0] + arguments[1]} else {return function (sec) {return fir +    Sec }  } }

13. answer the following question according to the code snippet below

for (var i = 0; i < 5; i++) {var btn = document.createelement (' button ');  Btn.appendchild (document.createtextnode (' Button ' + i));  Btn.addeventlistener (' Click ', function () {console.log (i);}); Document.body.appendChild (btn);}

1. Click Button 4, What will be output on the console?
2, give a way to meet the expected realization

1, Click any of the 5 buttons, are output 5
2, Reference Iife.

14. What does the following code output? Why?

var arr1 =  "john". Split (');  j o h nvar arr2 = arr1.reverse (); n h o jvar arr3 =  "jones". Split ('); j o n e  Sarr2.push (arr3); console.log ("array 1: length="  + arr1.length +  " last="  + arr1.slice ( -1)); console.log ("array 2: length="  + arr2.length +  "  last= " + arr2.slice (-1)); What will it output? You run down to know that may be unexpected in Your. 

The description for reverse () on the MDN is Jiangzi:

Description
The reverse method transposes the elements of the calling array object in place, mutating the array, and returning a refer ence to the Array.

Reverse () changes the array itself and returns a reference to the original array.

For slice usage please refer to: slice

15. What does the following code output? Why?

Console.log (1 + "2" + "2"), console.log (1 + + "2" + "2"), console.log (1 +-"1" + "2"), console.log (+ "1" + "1" + "2"); Conso Le.log ("a"-"b" + "2"); console.log ("a"-"b" + 2);

What to output, to run it yourself, you need to pay attention to three points:

    • Multiple numeric and numeric string blending operations are related to the position of the operand

Console.log (2 + 1 + ' 3 '); //' console.log ' (' 3 ' + 2 + 1); ' 321 '
    • A numeric string is converted to a number when the sign (+/-) is present in the number

Console.log (typeof ' 3 ');  Stringconsole.log (typeof + ' 3 '); Number

similarly, You can add a number to a string before the number

Console.log (typeof 3);  Numberconsole.log (typeof (' +3)); String
    • If the result of the operation cannot be converted to a number, NaN is returned

Console.log (' a ' * ' SD ');  NaNconsole.log (' A '-' B '); NaN

This graph is the rule of the Operation Transformation.

16. If the list is large, the following recursive code will cause a stack overflow. What if the code is modified without changing the recursive mode?

var list = Readhugelist ();     var nextlistitem = function () {var item = List.pop ();    If (item) {//process the list item ... nextlistitem (); }};

The solution in the original is to add a timer:

var list = Readhugelist ();     var nextlistitem = function () {var item = List.pop ();    If (item) {//process the list item ... setTimeout (nextlistitem, 0); }};

Please refer to question 10th for the principle of Solution.

Scan two-dimensional code, Changbai Mountain souvenir

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M01/8D/D7/wKioL1is2sWT_zy1AAH4lkqQ0cE920.png "title=" QQ Picture 20170217155213.png "width=" "height=" 208 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:200px;height:208px " alt= "wkiol1is2swt_zy1aah4lkqq0ce920.png"/>

This article from "no Special in, lifelong learning" blog, Please be sure to keep this source http://996665552.blog.51cto.com/11199442/1900067

You need to know some of the JavaScript face questions (medium)

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.