Front end interview questions · selection (continuous update)

Time:2020-1-17

2019.04.23Given the following code, how to modify it to make the picture width 300px? Note that the following code is not modifiable

<img style="width:480px!important;”>

Solution:

  • css3Zoom:

    transform(scale(0.625, 0.625));
  • js:

    document.getElementsByTagName("img")[0].setAttribute("style","width:300px!important;")
  • css: max-width: 300px;(I didn’t expect that…)
  • css: box-sizing: border-box; padding-left: 90px; padding-right: 90px;(I didn’t expect that…)

2019.03.22If the promise constructor is synchronous or asynchronous, what about the then method?

This is obvious,promiseConstructor issynchronizationExecutive,thenThe method isasynchronousExecuted.
I chose a slightly more complicated example from the answer:

const promise = new Promise((resolve, reject) => {
  console.log(1);
  resolve(5);
  console.log(2);
}).then(val => {
  console.log(val);
});

promise.then(() => {
  console.log(3);
});

console.log(4);

setTimeout(function() {
  console.log(6);
});
//Execution result: 124536

There will be aMacro task(macrotaskSumMicro task(microtask)The order of execution of thejavascriptOfevent loop(EventLoop)。

Last dry goods: this time, thoroughly understand the JavaScript execution mechanism. Ruan Yifeng’s detailed explanation of JavaScript operation mechanism: talk about event loop again. This problem is very important
What you don’t want to see can also be seen here:

First execute a macro task (the macro task is already running when JS is running), then execute the micro task, clear the micro task queue, then execute the macro task, then execute the micro task, clear the micro task queue

Common task types:

  • Micro task (jobs): promise / Ajax / object.observe (this method has been abandoned)
  • Macro task (task): settimout / script / Io / UI rendering

2019.01.22 ['1', '2', '3'].map(parseInt) what & why ?

Answer:1, NaN, NaN

The deformation of this question:

let unary = fn => val => fn(val)
let parse = unary(parseInt)
console.log(['1.1', '2', '0.3'].map(parse))

Answer:1.1, NaN, 0

Analysis:

parseIntDefinition and Usage

  • parseInt()Function resolves aCharacter string, and return ainteger
  • grammar

    • parseInt(string, radix)
    • parameter describe
      string Necessary. The string to be parsed is only converted to a valid number, and the decimal point calculation is invalid.
      radix Optional. Represents the cardinality of the number to resolve. The value is between 2 and 36. If you omit the parameter or its value is 0, the number is resolved on a 10-based basis. If it starts with ‘0x’ or ‘0x’, it will be based on 16. If the parameter is less than 2 or greater than 36, parseint() returns Nan.

array.map((e, index, arr) => {})The first parameter is the element, the second parameter is the index, and the third parameter is the currently traversed array.

The answer is coming:

parseInt('2', 1)1 is not between 2 and 36, return to Nan.
parseInt('3', 2)3 is not a valid number in binary. Nan is returned.

Give it a try:

['10','10','10','10','10'].map(parseInt);
// [10, NaN, 2, 3, 4]

Recommended Today

RCAST 35: add type to currency

– font ALT: Simsun; MSO font charset: 134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-face {font-family:”Cambria Math”; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Calibri; Variable; Ose-1: 216301111; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} /\* Style […]