//sample solution to Hangman Exercise 3
// Fill in this function!
function alterAt ( n, c, originalString ) {
if(n == 0){
return c + originalString.substr(1,(originalString.length-1));
} else if(n == (originalString.length-1)) {
return originalString.substr(0,(originalString.length-1)) + c;
} else if(n > (originalString.length-1) || c.length > 1) {
return 'invalid';
} else {
return originalString.substr(0,n) + c + originalString.substr(n+1,(originalString.length-n))
}
}
//for testing purposes
console.log(alterAt(0,'c','boggie'));
console.log(alterAt(2,'c','boggie'));
console.log(alterAt(5,'c','boggie'));
console.log(alterAt(6,'c','boggie'));
// Fill in this function!
function alterAt ( n, c, originalString ) {
if(n == 0){
return c + originalString.substr(1,(originalString.length-1));
} else if(n == (originalString.length-1)) {
return originalString.substr(0,(originalString.length-1)) + c;
} else if(n > (originalString.length-1) || c.length > 1) {
return 'invalid';
} else {
return originalString.substr(0,n) + c + originalString.substr(n+1,(originalString.length-n))
}
}
//for testing purposes
console.log(alterAt(0,'c','boggie'));
console.log(alterAt(2,'c','boggie'));
console.log(alterAt(5,'c','boggie'));
console.log(alterAt(6,'c','boggie'));
No comments:
Post a Comment