File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ /*
3+ Classical palindrome js version.
4+ "A palindrome is a word, number, phrase, or other sequence of
5+ characters which reads the same backward as forward, such as madam, racecar."
6+ https://en.wikipedia.org/wiki/Palindrome
7+ */
8+
9+ /**
10+ * Returns if the string is palindrome or not
11+ * case sensitive
12+ * @param {string } string. Required
13+ * @returns {boolean }
14+ */
15+ function isPalindrome ( string ) {
16+ const stringLength = string . length ;
17+
18+ if ( stringLength <= 1 ) return true ;
19+
20+ for ( let index = 0 ; index < stringLength / 2 ; index ++ ) {
21+ if ( string [ index ] !== string [ stringLength - 1 - index ] ) {
22+ return false ;
23+ }
24+ }
25+
26+ return true ;
27+ }
28+
29+ function isPalindromeTest ( ) {
30+ const stringList = [ '' , 'hi' , 'ANA' , 'racecar' ] ;
31+
32+ for ( string of stringList ) {
33+ console . assert ( isPalindrome ( string ) , 'Ups! %s is not palindrome :/' , string ) ;
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments