เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับสตริงและตัวเลข n และส่งคืนสตริงอื่นโดยลบอักขระ n ตัวแรกออกจากสตริง
ตัวอย่างเช่น −
หากสตริงเดิมคือ −
const str = "this is a string" and n = 5,
ผลลัพธ์ควรเป็น −
const output = "is a string"
มาเขียนโค้ดสำหรับฟังก์ชันนี้กัน −
ต่อไปนี้เป็นรหัส -
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)); ผลลัพธ์
ต่อไปนี้เป็นผลลัพธ์ในคอนโซล -
5389534759385 98345985734