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

สร้างสตริงแบบเวียนเกิดใน JavaScript


เราต้องเขียนฟังก์ชันเวียนเกิด เช่น pickString ที่รับสตริงที่มีการผสมผสานของอักษรและตัวเลข และคืนค่าสตริงใหม่ที่ประกอบด้วยเฉพาะอักษร

ตัวอย่าง

ถ้าสตริงคือ 'dis122344as65t34er' ผลลัพธ์จะเป็น: 'disaster'

ดังนั้น มาเขียนโค้ดสำหรับฟังก์ชันเวียนเกิดนี้

ตัวอย่าง

const str = 'ex3454am65p43le'; const pickString = (str, len = 0, res = '') => {    if(len < str.length){       const char = parseInt(str[len], 10) ? '' : str[len];       return pickString(str, len+1, res+char);    };    return res; }; console.log(pickString(str)); console.log(pickString('23123ca43n y43ou54 6do884 i43t')); console.log(pickString('h432e54l43l65646o')); console.log(pickString('t543h54is 54i5s 54t43he l543as53t 54ex87a455m54p45le'));

ผลลัพธ์

ผลลัพธ์ในคอนโซลจะเป็นดังนี้

example can you do it hello this is the last example