Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ลบตัวอักษรแรก k ตัวจากสตริงใน 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