Front End Interview Question - js - es5
data type
Basic Data Type Detection
Complete the JavaScript function, requiring the type of the parameter to be returned as a string.
Note: Only basic data types need to be detected.
function _typeof(value) { // Complete Code let str = Object.prototype.toString.call(value) let substring = str.substring(8, str.length -1).toLowerCase() return substring }
Detect complex data types
Complete the JavaScript functions and require that the first parameter be returned as a Boolean if it belongs to an instance of the second parameter object.
function _instanceof(left,right) { // Complete Code return Boolean(left instanceof right) }
Data Type Conversion
Complete the JavaScript function, requiring the concatenation of two numeric parameters to be returned as a string.
Example:
- _splice(223,233) -> "223233"
- _splice(-223,-233) -> "-223-233"
function _splice(left,right) { // Complete Code return left+String(right) return left+''+right; }
operator
Factorial
Complete the JavaScript function, requiring the factorial of the number parameter to be returned.
Note: The parameter is an integer greater than or equal to 0.
function _factorial(number) { // Complete Code if(number===1) return 1; return number*_factorial(number-1); }
absolute value
Complete the JavaScript function, requiring that the absolute value of the number parameter be returned.
function _abs(number) { // Complete Code return number>=0?number:-number }
power
Complete the JavaScript function by returning the value with the base number as the first parameter and the power as the second parameter.
// tip // Math.pow(0,0) // 1 // Math.pow(any,0) // 1 // Math.pow(1,any) // 1 // Math.pow(1,any) // 1 // Math.pow(-any,-any) // // Math.pow(-any,-any) // function _pow(number,power) { // Complete Code // return number**power // -1 ** any Error return Math.pow(number, power) }
**
x**n equals Math.pow(x,n), which is the n-th power of X
square root
Complete the JavaScript function, requiring the square root of the numeric parameter to be returned.
function _sqrt(number) { // Complete Code //Square root of JS Math object method sqrt(x) x return Math.sqrt(number); }
Remainder
Complete the JavaScript function, requiring the remainder of the number parameter divided by 2.
function _remainder(value) { // Complete Code return value % 2 return value - (value>>1)*2 }
Process Control
Complete the JavaScript function, requiring the week corresponding to the numeric parameter to be returned as a string.
Example:
- _ Getday (1) -> Monday
- _ Getday (7) -> "Sunday"
function _getday(value) { // Complete Code return "week"+['one','two','three','four','five','six','day'][value%7-1] }
Built-in Objects
Sort from large to small
Complete the JavaScript function, requiring the number in the array parameter to be sorted from large to small and returned.
function _sort(array) { // Complete Code return array.sort( (a,b) => b - a) }
Bubble sort
function _sort(array) { // Complete Code var temp; for(let i=0;i<array.length;i++){ for(let j=i+1;j<array.length;j++) if(array[i]<array[j]){ temp=array[i]; array[i]=array[j]; array[j]=temp; } } return array; }
Uppercase string
function _touppercase(string) { return string.toUpperCase(); }
Object Property Key Name
Complete the JavaScript functions, requiring the keyname of each property of the object to be output as an array.
Example:
- _keys({name:'nowcoder',age:7}) -> ['name','age']
Note: Only consider cases where the object properties are of the original data type.
function _keys(object) { // Complete Code return Object.keys(object) return Reflect.ownKeys(object) return Object.getOwnPropertyNames(object) } function _keys(object) { // Complete Code let arr=[] for (k in object) arr.push(k); return arr }
Object Number
Complete JavaScript functions, requiring that numeric parameters be returned as objects.
Example:
- typeof number === 'number' -> typeof _numbertoobject(number) === 'object'
function _numbertoobject(number) { // return {number} if( typeof number === 'number') return new Number(number); }
Object string
Complete JavaScript functions, requiring string parameters to be returned as objects.
Example:
- typeof string === 'string' -> typeof _stringtoobject(string) === 'object'
function _numbertoobject(string) { return new String(string); return {string} }
Remove spaces at both ends of a string
function _trim(string) { // Complete Code return string.replace(/^\s*|\s*$/g,'') return string.trim() }
Output Date
Complete the JavaScript function, requiring that the year-month-day corresponding to the timestamp parameter be output as a string.
Example:
- _date(1631159776311) -> '2021-9-9'
function _date(number) { // Complete Code let date = new Date(number) return date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate() return new Date(number).toLocaleDateString().replace('/','-').replace('/','-') }
Number rounding
function _int(value) { // Complete Code return value >> 0 return parseInt(value) return Number(String(value).split('.')[0]) }
Array inversion
function _reverse(array) { // Complete Code return array.reverse() let newArray = [] array.map((e, i) => newArray.push(array[array.length - 1 - i])) return newArray }
Array to String
Complete the JavaScript function, requiring that the parameter array be converted to string output.
Example:
- _join([1,'2',3]) -> "123"
Note: Only one-dimensional arrays need to be considered and the data type is the original data type.
function _join(array) { // Complete Code return array.join('') return array.toString().replace(/,/g,''); return array.reduce((per,cur,index,array)=>per+cur,''); }
Array Maximum
function _max(array) { // Complete Code return Math.max(...array); return Math.max.apply(null, array) return array.sort((a,b)=>b-a)[0] return array.sort()[array.length-1] }
Search Numbers
Complete the JavaScript function, requiring that the string parameter be returned as boolean with or without numbers.
function _search(string) { // Complete Code return /\d/g.test(string) for(var i=0;i<10;i++){ if(string.indexOf(i)!=-1){ return true } } return false }
Header Insert Element
Complete the JavaScript function by inserting the second parameter into the head of the first parameter array and returning it as an array.
// Error return array.unshift(value)//This returns the length of the new array, not the new array function _unshift(array,value) { // Complete Code return [value,...array] return [value].cancat(array) // return array.splice(0,0,value) // Error [] }
End Insert Element
function _push(array,value) { // Complete Code return [...array,value] return array.cancat([value]) array.push(value) return array // return array.push(value)//This return is actually the length of the array }
js-Location Lookup
Complete the JavaScript function, requiring that the first index value of the second parameter in the first parameter array be returned as a number.
Note: If the target value does not exist in the array, -1 is returned.
function _indexof(array,value) { // Complete Code return array.indexOf(value) return array.reduce((p,c,i) => p<0&&c===value? i:p, -1) }
Rounding Down
function _floor(number) { // Complete Code return Math.floor(number) }
Integer Inversion
Complete JavaScript functions, requiring output after inverting integer parameters.
Example:
- _reverse(0) -> 0
- _reverse(233) -> 332
- _reverse(-223) -> -322
function _reverse(number) { // Complete Code var temp = (number+'').split('') return number<0 ? -1*parseInt(temp.reverse().join('')) : parseInt(temp.reverse().join('')) }
String Search
Complete the JavaScript function, requiring a boolean return of whether the string first parameter contains the second parameter.
function _search(string,value) { // Complete Code return string.indexOf(value)==-1?false:true; return string.indexOf(value)!=-1 }
function
Parameter object
Complete the JavaScript function, requiring its parameter pseudo-array object to be returned.
function getArguments (a,b,c) { // Supplementary Code return arguments }
this points to
Complete the JavaScript function so that the fn function in the obj object returns the sum of the a and b attributes in the object.
var obj = { a: 1, b: 2, fn: function(){ // Complete Code return this.a+this.b } }