Given a string change the every second letter to an uppercase ‘Z’. Assume there are no space.
let string be javascript
Example output: jZvZsZrZpZ
OR each letter on a new line
HERE IS MY CODE
let newarr = str1.split('');
let newword = "Z";
for(i = 1; i < newarr.lenth; i += 2){
newarr[i] = newword; newword.join('');}
console.log(newword);
Here is my other code I tried
let newarr = str1.split('');
for(i = 1; i < newarr.lenth; i+=2){
newarr.replace('newarr[i]' , 'z'); newarr.join('');}
console.log(newarr);
Which code closer to correct ? And where Did I go wrong for both codes??
2
Answers
To summarize the things that went wrong:
I made a simple approach. Keep in mind this is not the most optimal because I unnecessary loop unchanged letters, which you have done better:
With a more optimal loop:
You could do it with a Regular Expression: