Create a function makeHistory that accepts a number (which will serve as a limit), and returns a function (that will accept a string). The returned function will save a history of the most recent "limit" number of strings passed into the returned function (one per invocation only). Every time a string is passed into the function, the function should return that same string with the word 'done' after it (separated by a space). However, if the string 'undo' is passed into the function, then the function should delete the last action saved in the history, and return that delted string with the word 'undone' after (separated by a space). If 'undo' is passed into the function and the function's history is empty, then the function should return the string 'nothing to undo'.
// 下文是解决方案,当时想的难点在于如何计数,后来突然想明白要把之前的清空。
var arr = [];
let count = 1;
function ok(str){
if(str!=="undo"){
count = 1;
arr.push(str);
let ret = str + " "+"done";
return ret;
}
else{
if(arr.length>0 && count<=limit){
count++;
let delt = arr.pop();
delt = delt +" "+"undone";
return delt;
}
else{
return "nothing to undo"
}
}
return ok;
}
// /*** Uncomment these to check your work! ***/
const myActions = makeHistory(2);
console.log(myActions('jump')); // => should log 'jump done'
console.log(myActions('undo')); // => should log 'jump undone'
console.log(myActions('walk')); // => should log 'walk done'
console.log(myActions('code')); // => should log 'code done'
console.log(myActions('pose')); // => should log 'pose done'
console.log(myActions('undo')); // => should log 'pose undone'
console.log(myActions('undo')); // => should log 'code undone'
console.log(myActions('undo')); // => should log 'nothing to undo'