We are required to write a JavaScript function that takes in a string and a number n and returns another string with first n characters removed from the string.
For example −
If the original string is −
const str = "this is a string" and n = 5,
then the output should be −
const output = "is a string"
Let’s write the code for this function −
Following is the code −
const mobileNumber = '+915389534759385'; const secondNumber = '+198345985734'; const removeN = (str, num) => { const { length } = str; if(num > length){ return str; }; const newStr = str.substr(num, length - num); return newStr; }; console.log(removeN(mobileNumber, 3)); console.log(removeN(secondNumber, 2));
Output
Following is the output in the console −
5389534759385 98345985734