- Write a function in JavaScript that enters an int and returns the string in reverse order of an integer. For example, if you enter integer 1234, the string 4321 is returned. You must use recursive function calls, not global variables, input functions must have only one argument passed in, and must return a string. (Billie Billie algorithm)
function reverse (n) {
if (typeof n === 'string') {
console.log('Please enter number type');
return;
}
let s = n % 10; / / 4
let s_string = s + ' ';
if ((n / 10) > =1) {
s_string = s_string + reverse((n-s) / 10);
}
return s_string;
}
Copy the code
- Given the following array:
var arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];
Write a program to flatten the array and divide the repeated part of the data, and finally get an ascending and non-repeating array (Cvisit algorithm)
function flatten (arr) {
let newArr = [];
for(let i = 0; i < arr.length; i++) {
let a = arr[i];
if (Array.isArray(a)) {
// If it is an array
newArr = flatten(newArr.concat(a));
} else {
if (newArr.indexOf(a) > -1) continue; newArr.push(a); }}return newArr.sort((b, c) = > b - c);
}
Copy the code
or
Prototype. Flat (deep)
// go to new Set
/ / the sort order
function flatten2 (arr) {
return Array.from(new Set(arr.flat(Infinity))).sort((a, b) = > a - b );
}
Copy the code