เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับสตริงและตัวเลข เช่น k และคืนค่าสตริงอื่นโดยลบอักขระ k ตัวแรกออกจากสตริง
ตัวอย่างเช่น หากสตริงเดิมคือ −
const str = "this is a string"
และ,
n = 4
ผลลัพธ์ควรเป็น −
const output = " is a string"
ตัวอย่าง
รหัสสำหรับสิ่งนี้จะเป็น −
const str = 'this is a string';
const removeN = (str, num) => {
const { length } = str;
if(num > length){
return str;
};
const newStr = str.substr(num, length - num);
return newStr;
};
console.log(removeN(str, 3)); ผลลัพธ์
เอาต์พุตในคอนโซล −
s is a string