I need answer for this Quickly! Please?

create a function on String that takes an integer n as parameter. If n is an even number, it should return the string in uppercase. If n is odd, it should return the string in lowercase.

Basically, you’ll need to write a script to make the following possible.

'stringname'.functionName(5) should return 'stringname'
'stringname'.functionName(4) should return 'STRINGNAME'

Welcome to the forums, @akhilmukkamala.

That sounds like some kind of homework assignment. (That’s not a problem; we’re happy to help with any questions.)

How far have you got with this, and at what point do you need help? Please post whatever code you have so far for others to see. (To display code correctly here, you can highlight your code, then use the </> button in the editor window, which will format it, or you can place three backticks ``` (top left key on US/UK keyboards) on a line before your code, and three on a line after your code. I find this approach easier, but unfortunately some European and other keyboards don’t have that character.)

The following should do the trick:

String.prototype.functionName=function(i){return String(this)["to"+(i%2?"Low":"Upp")+"erCase"]();}
2 Likes

Thanks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.