Javascript를 배우고 있습니다. 매일 배운 것을 이해한만큼 정리해봅니다.
function reverse(string) {
// recursive stuff should be located here
}
/* writing in a pseudocode
the original probelm: to reverse string
One step simpler problem: to reverse string[1] ~ string[last]
the complex problem: to reverse string[0]
*/
function reverse(string) {
if(string.length < 2) {
return string;
}
}
function reverse(string) {
if(string.length < 2) {
return string;
}
reverse(string.slice(1));// somehow it will work
}
function reverse(string) {
if(string.length < 2) {
return string;
} else {
return reverse(string.slice(1) + string[0]);
}
}
function printArr(array) {
// recursive stuff should be located here
}
/* writing in a pseudocode
the original probelm: to print the entire array
One step simpler problem: to print array[1] ~ array[last]
the complex problem: to print array[0]
*/
function printArr(array) {
if(array.length < 2 && !Array.isArray(array[0])) {
return array[0];
}
}
function printArr(array) {
if(array.length < 2 && !Array.isArray(array[0])) {
return array[0];
}
let newArr = array.slice();
newArr.shift();
printArr(newArr);// somehow it will work
}
function printArr(array) {
if(array.length < 2 && !Array.isArray(array[0])) {
return array[0];
} else {
let newArr = array.slice();
newArr.shift();
if(Array.isArray(array)) {
return printArr(array[0]) + printArr(newArr);
} else {
return array[0] + printArr(newArr);
}
}
}
function isPalindrome(str) {
// recursive stuff should be located here
}
/* writing in a pseudocode
the original probelm: to check the entire characters
One step simpler problem: to check str[1] ~ str[last-1]
the complex problem: to check whether str[0] is same as str[last-1]
*/
function isPalindrome(str) {
let leng = str.length;
if(leng <= 1) {
return true;
}
}
function isPalindrome(str) {
let leng = str.length;
if(leng <= 1) {
return true;
}
return isPalindrom(str.slice(1, leng - 1));
// somehow it will work
}
function isPalindrome(str) {
let leng = str.length;
if(leng <= 1) {
return true;
} else {
if(leng % 2 === 0) {
return (str[0] === str[leng - 1]) && isPalindrome(str.slice(1, leng - 2));
}
return(str[0] === str[leng - 1]) && isPalindrome(str.slice(1, leng - 1));
}
}
function mapArr(arr, cb, result = []) {
// recursive stuff should be located here
}
/* writing in a pseudocode
the original probelm: to change the entire elements
One step simpler problem: to change array[1] ~ array[last]
the complex problem: to change array[0];
*/
function mapArr(arr, cb, result = []) {
if(!arr.length) {
return arr;
}
}
function mapArr(arr, cb, result = []) {
if(!arr.length) {
return arr;
}
let newArr = arr.slice();
newArr.shift();
mapArr(newArr, cb, result); // somehow it will work
}
function mapArr(arr, cb, result=[]) {
if(!arr.length) {
return arr;
}
let newArr = arr.slice();
newArr.shift();
result.push(cb(arr[0]));
mapArr(newArr, cb, result);
return result;
}