Table of contents
2. Among the following expressions, the result is NaN ()
1. Multiple choice questions
1. Assuming that HTML has a button with the id btn, what color will it eventually turn into after the following operations?
document.getElementById('btn').style = 'background: blue';
document.getElementById('btn').style = 'background: red';
Promise.resolve().then(() => {
document.getElementById('btn').style = 'background: black';
})
A. From blue to red to black
B. The color will not change
C、red
D、black
Correct answer: D Your answer: C
Parse:
(1) First of all, it is clear that UI rendering is a macro task . According to the event loop model, the overall main code is executed first. During this period, the style changes frequently but has not been rendered , so there will be no color change. The task is the promise callback function, which finally changes the style to black , and then executes the UI rendering , which is the final color
(2) JS will block DOM rendering, recommended articles
It turns out that CSS and JS block DOM parsing and rendering in this way - Nuggets
2. Among the following expressions, the result is NaN ()
A、123 + null
B、123 / 0
C、123 + '1';
D、123 + undefined;
Correct answer: D Your answer: B
Parse:
(1) option
Option A: For the "+" operation, if one end is a Number type and the other end is a primitive data type, the other end will be converted to a Number type, and then added, null will be converted to 0, and the result will be 123
Option B: 123/0 results in Infinity
C option: For the "+" operation, if one end is a string, the other end will be converted to a string for connection between strings, so the result is 1231
D option: For the "+" operation, undefined will be converted to NaN, and the result will also be NaN
<script>
console.log(123 + null);//123
console.log(123 / 0);//Infinity
console.log(123 + '1');//1231
console.log(123 + undefined);//NaN
</script>
(2) In JavaScript, 0 is allowed as a divisor, only 0/0 results in NaN, and other values /0 result in Infinity
<script>
console.log(0/0);//NaN
console.log(1/0);//Infinity
console.log('1'/0);//Infinity
console.log('a'/0);//NaN
</script>
3. You want to update the following elements through XMLHttpRequest, that is, use div to display the status. Which of the following codes is correct?
<div id="statusCode"></div>
A、var myDiv = document.getElementById ("statusCode"); myDiv.innerHTML = req.statusCode;
B、var myDiv = document.getElementById ("statusCode"); myDiv.innerHTML = req.status;
C、var myDiv = document.getElementById ("statusCode"); myDiv.setStatus (req.statusCode);
D、var myDiv = document.getElementById ("statusCode"); myDiv.status = req.status;
Correct answer: B
Parse:
(1) status and statusText
The status attribute returns the status code, which is a number
The statusText property returns the status code and description, a string
(2) The meanings of the readyState and status codes of the XMLHttpRequest object:
① readyState has five states:
- 0 (uninitialized): The (XMLHttpRequest) object has been created, but the open() method has not been called;
- 1 (loading): the open() method has been called, but the request has not been sent yet;
- 2 (loading complete): The request has been sent;
- 3 (interactive): Part of the response data can be received ;
- 4 (Complete): All data has been received and the connection has been closed .
② status is actually an auxiliary status judgment, but status is more of a status judgment on the server side. The common status is as follows:
- 1xx——Information category, indicating that a web browser request has been received and is being further processed. For example, 100: the client must continue to issue requests; 101: the client requires the server to convert the HTTP protocol version according to the request
- 2xx——Success, indicating that the user request was correctly received, understood and processed. For example, 200: OK; 201: Prompt to know the URL of the new file
- 3xx - Redirection, indicating that the request was not successful and the client must take further action. For example, 300: The requested resource is available in multiple places; 301: Delete the requested data
- 4xx——client error, indicating that the request submitted by the client has an error. For example, 404: NOT Found means that the document referenced in the request does not exist.
- 5xx——Server error, indicating that the server cannot complete the processing of the request. For example, 500, the server generated an internal error
2. Programming questions
1. Return whether the first parameter of the string contains the second parameter in the form of boolean
Parse:
(1) search () method
<script>
let string = '24516'
let value = 3
function _search(string, value) {
return string.search(value) === -1 ? false : true
}
console.log(_search(string, value));
</script>
(2) indexOf () method
<script>
let string = '24516'
let value = 3
function _search(string, value) {
return string.indexOf(value) === -1 ? false : true
}
console.log(_search(string, value));
</script>
(3) for () loop
<script>
let string = '24516'
let value = 3
function _search(string, value) {
let arr = [...string]
for(let i of arr){
if(i == value){
return true
}
}
return false
}
console.log(_search(string, value));
</script>