Palindrom-v2

Palindrom v2

Given a string, return a new string with the reversed order of characters.

Example:

console.log(reverse(‘hannah’)) true
console.log(reverse(‘apple’)) false

function reverse(str) {
	return str.split('').every((char, i) => {
		return char === str[str.length - i -1];
		});
	}
console.log(reverse('hannah'))
console.log(reverse('apple'))

For better explanation of this solution will be graphical
A B C B A
every function works on arrays so we split(‘’). Then
every checks in this order.
is A === A(last one) yes ok palindrom
is B === B(before last) yes ok palindrom
is C === C (C is only one )so palindrom.
But unfortunatelly
the list goes on
is B ==== B(index 1) palindrom

is A ==== A (index 0) palindrom

comments powered by Disqus