There are certain things that you can do with this
1) You can spread string in an array because str
is a string and strings are iterable in JS.
let arr = [...str];
2) You can use spread in-place of slice to make it more readable
let forward = [...arr];
3) Since reverse is in place, so you can spread it into a new array.
let backward = [...arr.reverse()];
4) You can have multiple options here first to use join
and then compare because strings are primitive and compared with the value that is already answered.
So you can also use every here:
forward.every((s, i) => s === backward[i]);
var str = "level";
let arr = [...str];
function repeat(arr) {
let forward = [...arr];
let backward = [...arr.reverse()];
console.log(forward);
console.log(backward);
return forward.every((s, i) => s === backward[i]);
}
var result = repeat(arr);
console.log(result);