'change even character in string with the for loop
hello everyone please i'am a begginer in javascript and i found this challenge , but i don't know how to solve it
let str1 = "javascript";
// Example output:
// jZvZsZrZpZ OR each letter on a new line
// HINT: You can use if((i+1) % 2 == 0) to check for even indexes
for (let i = 0; i < str1.length; i++) {
if ((i + 1) % 2 === 0) {
str1[i].replace(str1[i],"z")
}
}
can you explain this for me please i try this soltuion but it doesn't work
Solution 1:[1]
This should work for you. JsFidle
let str1 = "javascript";
const res = [...str1].map((letter, index)=>{
return index % 2 != 0 ? "Z" : letter
})
console.log(res.join(""))
Solution 2:[2]
let str1 = "javascript";
// Example output:
// jZvZsZrZpZ OR each letter on a new line
var a = str1.split("");
let replaceChar = function(char){
for(let i=1;i<str1.length;i++){
if((i+1)% 2 === 0)
a[i] = char
}
return a.join("");
}
console.log( replaceChar("Z"))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | OndrejHj04 |
Solution 2 | bharat bhushan |