【发布时间】:2021-03-03 04:14:27
【问题描述】:
我需要一个函数,它接受一个字符数组并返回相同的数组,但相反,没有“*”字符。
我尝试了很多代码都没有成功。这是我最后一次尝试(对不起,如果没有意义,我正在学习 JS)
function laClaveSecreta(carac){
let new_str=[];
for(i=0;i<carac.length;i++){
if(carac[i]==="*"){
new_str=new_str.push(carac.replace(/[*\s]/g, ''));
}
return new_str.reverse();
}
}
//Running the function with this parameter
laClaveSecreta( [ "s", "*", "e", "n", "u", "l", " ", "s", "*", "e", " ", "a", "í", "*", "d", " ", "l", "*", "E", "*"] )
//Result I am looking for
"El día es lunes"
【问题讨论】:
-
先将数组转换为字符串会更容易,然后您可以在整个字符串上使用
replace。carac.reverse().join("").replace(/[*]/g, "")
标签: javascript string function reverse