I want to write a function that returns alternating characters, so for instance:
"hello world".toAlternatingCase() === "HELLO WORLD"
"HELLO WORLD".toAlternatingCase() === "hello world"
"HeLLo WoRLD".toAlternatingCase() === "hEllO wOrld"
Here is my try
const alternatingCase = function (string) {
let newword = "";
for (i = 0; i<string.length; i++) {
if (string.charAt(i).toUpperCase())
{
newword+= string.charAt(i).toLowerCase()
}
else
{
newword+=string.charAt(i).toUpperCase()
}
}
return newword
}
When invoked, the function unfortunately only returns the string itself, not sure what I'm doing wrong though, thanks very much for reading or even helping out!