How do I add input validation to this function so that the test cases work.
Test cases 1-3 should output a truncated string (or the original, if the “targetLength” value isn’t greater than the string length) , while test cases 4 and 5 will output the string “Invalid Input”.
Below is the function:
function truncate(inputString, targetLength){
return inputString.substr(0, targetLength);
}
//test cases
var x = “It happened just a week ago”;
var y;
console.log(“1: ” + truncate(x, 10));
console.log(“2: ” + truncate(x)); // should output the unmodified string
console.log(“3: ” + truncate(10, 10));
console.log(“4: ” + truncate(y, 10));
console.log(“5: ” + truncate(x, “Bob”));