varreplaceSpace=function(s){let temp = s.split(" ");let result ='';for(let i =0; i < temp.length -1; i++){
result += temp[i]+"%20";}return result + temp[temp.length -1];};
console.log(replaceSpace("We are happy."));
3.遍历字符串,字符串拼接
varreplaceSpace=function(s){let result ="";for(let i =0; i < s.length; i++){if(s[i]===" "){
result +="%20";}else{
result += s[i];}}return result;};
console.log(replaceSpace("We are happy."));