PDA

View Full Version : byte economy challenge!


ca_redwards
01-03-2003, 03:20 AM
Let us revisit Beetle's Code Challenge! (http://www.codingforums.com/showthread.php?threadid=12285&goto=newpost) with a new twist:

Implement the string reversing function in as few characters as possible! (And use all the semicolons you like...)

Use this declaration:


function convert(t){ /*your most compact code here!*/ }


Assume that the t parameter is a textfield element thusly...


<html>
<head>
<title>Contest</title>
<script type="text/javascript">

function convert( inputObject )
{
var stringIn = inputObject.value;
var stringOut = "";
for ( var i = stringIn.length; i >= 0; i-- )
{
stringOut += stringIn.charAt(i);
}
inputObject.value = stringOut;
}

</script>
</head>

<body>

<form>
<input type="text" name="text1" size="80" />
<br />
<input type="button" value="convert" onclick="convert( this.form.text1 );" />
</form>

</body>
</html>


On your mark, get set, code!

jkd
01-03-2003, 03:42 AM
Before mordred grabs it:

t.value=t.value.split('').reverse().join('')

:p

beetle
01-03-2003, 04:05 AM
I'd be impressed to see it go smaller than that.

whammy
01-03-2003, 11:33 PM
Me too! If I ever need to reverse a string (although I can't imagine why), I'll definitely be using that version... ;)