In this tutorial, you will write JavaScript code that will print characters of a string at odd positions.
//JavaScript program to print characters of a string at odd positions let str = "JavaScript"; for(let i=0; i<str.length; i++){ if(i%2!==0){ console.log(str[i]); } }
Output
a a c i t
Explanation of the code:
1. You know that an odd number is not divisible by 2. We have used this concept in the if statement.
2. Using the for
loop, we will iterate the string. Inside the loop, we will check if an index is divisible by 2 or not. If it is not divisible, we will print characters at odd positions.