Convert the string abc-def-ghi to camel case
Here our idea is to use string methods and regular expressions
const str = 'abc-def-ghi';
const camelCaseStr = str.replace(/[-_][^-_]/g, match => match.charAt(1).toUpperCase());
console.log(camelCaseStr); // 'abcDefGhi'
replace
The method is used here , which accepts two parameters: the first parameter is the regular expression to match, and the second parameter is what to replace. Specifically, the regular expression /[-_][^-_]/g
matches any occurrence of a hyphen or underscore immediately followed by a non-hyphen or underscore. Then, the role of the callback function is to convert the second character in the matched characters to an uppercase letter, and return this new string to replace the original match. The final result is 'abcDefGhi'
a string in camel case.
Find the repeated elements in the array arr [1, 2, 4, 4, 3, 3, 1, 5, 3] (regardless of the return order)
const arr = [1, 2, 4, 4, 3, 3, 1, 5, 3];
const result =new Set(arr.filter((item, index) => {
return arr.indexOf(item) !== index;
}))
Here we use the filter method, which needs to pass in two values, where the first parameter item
represents the element currently being traversed, and the second parameter index
represents the index position of the current element in the array. The function of this callback function is to determine whether the current element is repeated in the array, and return true if it is, otherwise return false. This will find duplicate elements.
But in this way, there are new duplicates in the new array we found, we can use Set to remove the duplicates.
Array deduplication, remove arr = [1,1,8,8,12,12,15,15,16,16]; repeated elements in the array
var arr = [1,1,8,8,12,12,15,15,16,16];
function arr1(arr){
return Array.from(new Set(arr))
}
console.log(arr1(arr));
arr1
In the function firstly use the Set object in JavaScript to deduplicate the input array. The Set object is a collection of unique values, which can be used to quickly filter out unique elements from an array. Here a Set object is new Set(arr)
created with the input array arr
passed in as a parameter.
Then use Array.from()
the method to convert the Set object into a new array, and use it as the return value of the function. Array.from()
method to convert an array-like or iterable object into a new true array instance.
Write a method to remove all spaces in a string. For example: ' a bc ', need to output: 'abc'.
var str=' a bc '
function space(str){
return str.replace(/\s/g,'')
}
console.log(space(str));
Regular expression global match replaces spaces with empty strings
Remove HTML tags from string
function html_tags(str){
return str.replace(/<\/?[^>]+(>|$)/g,'')
}
console.log(html_tags('<p><span>JAVASCRIPT</span></p>'));
This question tests the understanding of regular expressions
<
and >
match opening and closing angle brackets respectively.
\/?
Matches an optional slash to handle the difference between opening and closing tags.
[^>]+
Matches one or more characters that are not angle brackets, i.e. tag names and attributes.
(>|$)
Matches an angle bracket and an optional curly brace to handle the case of nested tags.
g
Indicates a global match pattern, that is, finds all matches in the entire string instead of just the first match.
Please write code to calculate how many days are between the time stamps 1630257661126 and 1638257589478?
const timestamp1 = 1630257661126;
const timestamp2 = 1638257589478;
const dayInMillis = 24 * 60 * 60 * 1000; // 一天的毫秒数
const daysDiff = Math.abs(Math.floor((timestamp2 - timestamp1) / dayInMillis)); // 计算两个时间戳之间相差的天数
console.log(daysDiff);
Specifically, first define a constant dayInMillis
representing the number of milliseconds in a day. Then get the difference in milliseconds between the two timestamps by subtraction, and use Math.floor()
the method round down to get the number of days difference, and finally use Math.abs()
the method to take the absolute value to ensure that the result is a positive number.
Note that Math.floor()
the method because we only need to consider the complete number of days between two timestamps, and do not need to consider time differences such as hours and minutes that are accurate to less than seconds. If you need to calculate the date difference more accurately, you can use the third-party library moment.js or day.js.
Display the following JSON to the page:
let obj= { "key" : "I love you", "key1" : "forever" } Display effect: <div> <div>I say: I love you</div> <div>You say: forever A</div> </div>
let obj= {
"key" : "我爱你",
"key1" : "一生一世"
}
let html=`<div>
<div>我说:${obj.key}</div>
<div>你说:${obj.key1}</div>
</div> `
document.write(html)
ES6 template string syntax defines a string template html
named
Finally document.write()
, the template string is html
output to the document using the method
Implement the Fibonacci sequence using JavaScript. Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13. The characteristic of this sequence is that starting from the third item, each item is equal to the sum of the previous two items.
Implement the Fibonacci sequence using recursion:
function fibonacci(n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
A function fibonacci
named , which accepts an integer parameter n
, indicating that the n
item . If n<=1
so, return directly n
; otherwise, call itself recursively to calculate the sum of the first two items and return the result.
Use a loop to implement the Fibonacci sequence:
function fibonacci(n) {
let prev = 0;
let curr = 1;
for (let i = 2; i <= n; i++) {
const next = prev + curr;
prev = curr;
curr = next;
}
return curr;
}
In this implementation, we use two variables prev
and curr
to hold the first two terms of the Fibonacci sequence, respectively. Then use a loop to calculate from the third item to n
the item and update the values ofprev
and in turn. curr
Finally, return the value of the current item.