in the OP:
function (string)
In a function declaration, a name is mandatory, so:
function reverse(string)
{
var length = string.length;
There is no need for length, just initialise i in the for loop to string.length.
var reversed = [];
var joined = ("");
There is no need for the grouping operator:
var joined = "";
But there's no need for joined at all.
for (var i = length; i > 0; i--){
reversed.push(string.charAt(i-1));
}
for (i = 0; i < (length) ; i++){
joined += (reversed[i]);
}
why put the characters into an array, then iterate over the array to put them back into a string? Immediately after the first loop you could do:
return reversed.join('');
return joined ;
You omitted the closing bracket:
}
As noted in comments, you can use split, reverse and join, or a recursive function. You could also use split and forEach, but probably faster to use a plain loop as you've done:
function reverse(s) {
var result = '',
i = s.length;
while (i) {
result += s[--i];
}
return result;
}
Though +=
can be slow in some older browsers. Maybe test performance - oh look, it's been done… and a plain loop is fastest by a long way. ;-)