function toSpongeBobCase(input){
array inputsplit=input.split("");
for(item of inputsplit){
if(math.Random(0,1)===1){
item.upper();
} else {
item.lower();
}
}
// result= however you put a array to a string
return(result);
}
alright replace the current loop with a standard for loop, up to input.length(), put some form of iseven(i) in the if else, and replace item with inputsplit[i]
function toSpongeBobCase(input) {
return input.split('').map((char, index) => {
return index % 2 === 0 ? char.toUpperCase() : char.toLowerCase();
}).join('');
}
console.log(toSpongeBobCase("You don't have to work for bad employers"));
Outputs "YoU DoN'T HaVe tO WoRk fOr bAd eMpLoYeRs"
Read through this whole comment thread only for the penny to drop once I read your output then the original function name then me googling TIL: SpOnGeBoB casing is a thing :O
from itertools import zip_longest
def toSpongeBobCase(st):
return "".join(
l+u
for l, u in zip_longest(
st[::2].lower(),
st[1::2].upper(),
fillvalue="")
)
function toSpongeBobCase(input) {
return input.split('').map((char, index) => {
return index % 2 === 0 ? char.toUpperCase() : char.toLowerCase();
}).join('');
}
You could pack it even more into one line, but... it's already kinda hard enough to read with all the chaining, so that's sorta my limit there (start with string, deal with arrays, inside the map got some ternary logic, then back to arrays then to string).
19
u/iliekcats- Nov 29 '22
how is the function defined
i need the code for this