Arrangement of byte interview questions (Part 2)

0.1 + 0.2

JavaScript does not define different types of numeric data types, but always follows the international IEEE 754 standard to store numbers as double-precision floating-point numbers.

This format stores numbers in 64 bits, where the number (fraction) is stored in bits 0 to 51, the exponent is stored in bits 52 to 62, and the sign is stored in bit 63.

0.1 and 0.2 will loop indefinitely after being converted to binary

However, due to the IEEE 754 mantissa limit, the extra bits need to be truncated

So there will be corresponding errors in the results after calculation

Explain the printing of the following codes

for (var a = 0; a < 200; a++) {
    
    
	setTimeout(function () {
    
    
		console.log(a)
	}, 1000)
}

Print 200 200, if it is changed to let, it will print 0-199, or write a self-calling function internally

for (var i=0; i<10; i){
    
    
  (i → {
    
    
    setTimeout(()→{
    
    
      console.log(i)},1000 )
  })(i)
}

Requirements: Scan the code to log in, state the technical design

  • The PC browser requests a UUID from the server, and the server sets an expiration time for the UUID.

  • Scan the QR code on the mobile phone, take an interface address of the UUID, access the interface address, stitch the login status of the mobile phone to access the server, and save the successful status after the server confirms the success.

  • The PC browser polls to tap to access the server login status. If the login is successful, the page will be redirected, and the QR code will expire.

How to achieve the effect of scratching music, judge that 80% is scratched

  • ctx.beginPath(); ctx.arc(x, y, 20, 0, Math.PI * 2); ctx.fill();

  • Generate a canvas with background color, drawing without color is equal to eraser

  • canvas.getContext(‘2d’).getImageData(0,0,w,h).data

  • The for loop traverses the count if i, data[i] == 0, and judges the ratio of the count to data.length. When it reaches 80%, it is judged to have been scratched.

The difference between process and thread, what are the common processes in Chrome, if I have a long time-consuming synchronous computing task, how to make JS code achieve the effect of multi-threaded concurrent execution?

  • The process is the smallest unit of cpu resource allocation

  • Thread is the smallest unit of cpu scheduling

  • Thread is a program running unit based on the process. There can be multiple threads in a process

  • Single-threaded and multi-threaded refer to single and multiple in a process

  • Simply understand, every time you open a Tab page, it is equivalent to creating an independent browser process

  • Common processes: 1 main browser (Browser) process, 1 GPU process, 1 network (NetWork) process, multiple rendering processes, and multiple plug-in processes.

  • Common threads: GUI rendering thread, JS engine thread, event trigger thread, timing trigger thread, asynchronous http request thread

  • The main thread uses the new command to call the Worker() constructor to create a new Worker thread.

  • The parameter of the Worker() constructor is a script file, which is the task to be performed by the Worker thread. Since Worker cannot read local files, this script must come from the network.

  • Multithreading uses promise.all() and promise.race() for concurrency.

Given a string as follows, please count the most frequent letters and times in the string

function findMaxDuplicateChar(str) {
    
    
	let maxChar = ''
	let maxValue = 1
	let s = str.replace(/\s+/g, '')
	let obj = new Map()
	for (let i = 0, len = s.length; i < len; i++) {
    
    
		if (obj[s[i]]) {
    
    
			obj[s[i]]++
		} else {
    
    
			obj[s[i]] = 1
		}
	}
	console.log(obj)
	for (let key in obj) {
    
    
		if (obj[key] > maxValue) {
    
    
			maxValue = obj[key]
			maxChar = key
		}
	}
	return {
    
    
		maxChar,
		maxValue,
	}
}
const str = 'this is a fe test at toutiao on September'
findMaxDuplicateChar(str)

Guess you like

Origin blog.csdn.net/qq_40289624/article/details/111904072