Alternating characters

Write a function that returns a string of length Num consisting of alternating characters: “a” and “b”, starting with an “a” character.
For example, giver Num=5, your function should return “ababa”.

function altChars(Num) {
    let result = '';
    for (i = 0; i < Num; i++) {
        if (i % 2 == 0) {
            result += 'a';
        } else {
            result += 'b';
        }
    }
    return result;
}

Leave a Reply

Your email address will not be published. Required fields are marked *