Stuck and need some help with my palindrome function

Hi, i’m trying to tackle a task where you have to make a function to check if a word is a palindrome or not by using loops,

My current code attempt so far

function palindrome(word){

let lowercase = word.toLowerCase();
let letters = lowercase.split('');
let empty_arr = [];

for ( let i = letters.length-1; i >=0; i--){
empty_arr.push(letters[i]);
empty_arr.join('');
}

if ( empty_arr == word) {
    console.log('its a palindrome')
} else {
    console.log('Not a palindrome')
}

}

My thought process
I’m trying to split the argument string’s individual letters into an array then reverse loop through it backwards so they can be pushed into a new empty array and then joined to be converted into a new string. I then want to use that joined string to compare it to the argument input with an equality operator.

But my code can’t seem to join the new array i made of the pushed letters, any help and some feedback with what I’m done wrong?

You are calling join too soon, move it out of the loop. You also were checking your reversed array vs the original word which would always fail because you lowercased the it for the reverse array.

function palindrome (word) { let wordCheck = word.toLowerCase(); let reversed = wordCheck.split('').reverse().join(''); if (reversed === wordCheck) { console.log('its a palindrome') } else { console.log('Not a palindrome') } } let examples = ["Anna", "Dog", "Civic", "Blog"]; examples.forEach(el => palindrome(el));
2 Likes

I see, i tried to join it outside the for loop block but it still will not join? ( i also made that sure its checking the lowercased argument now )

function palindrome(word){ let lowercase = word.toLowerCase(); let letters = lowercase.split(''); let empty_arr = []; for ( let i = letters.length-1; i >=0; i--){ empty_arr.push(letters[i]); } empty_arr.join(''); console.log(empty_arr); if ( empty_arr == lowercase) { console.log('its a palindrome') } else { console.log('Not a palindrome') } } palindrome('radar');

Also thank you for showing me a good solution for a palindrome function :slight_smile: however the task im attempting is specifically asking to use loops so thats why I’m using a for loop.

Documentation for join:

The join() method creates and returns a new string by concatenating all of the elements in an array …

// You wrote:
empty_arr.join('');
// Change to:
empty_arr = empty_arr.join(''); 
// or you could assign to some other variable.
2 Likes

Thanks that made it work, but the documentation wasn’t clear that I have to assign the method into a variable for it to return the string, is this the same for every other array methods?

There are some methods which mutate/change the original array. There are some methods which don’t change the original array, but return new array. The documentation details information about syntax, parameters and return values.
The statement empty_arr.join(''); did return a string, but if you don’t assign the return value to a variable or print to the console, then you aren’t using the return value to do something.
For example,

let arr = ["ab", "cd", "ef"];
console.log(arr.join('')); // Output: "abcdef"

// Even though we didn't assign the result of join
// to a variable, join did return a string which
// was then printed by console.log 



To see list of which methods returns new arrays and which mutate original array, see the section in documentation: Copying methods and mutating methods

let arr = ["tomato", "potato", "cheese", "carrot", "rice", "zucchini"];

// THESE METHODS DON'T MODIFY ORIGINAL BUT RETURN NEW ARRAY.
let filtArr = arr.filter((word) => word.length == 6);
console.log(arr); // Output: ["tomato", "potato", "cheese", "carrot", "rice", "zucchini"];
console.log(filtArr); // Output:  [ 'tomato', 'potato', 'cheese', 'carrot' ]
// The filter method doesn't mutate original array. It returns new array.
// If we don't assign the returned array to a variable or print it to console,
// then we aren't doing anything with the returned array.

let mapArr = arr.map((word) => "<" + word + ">");
console.log(arr); // Output: ["tomato", "potato", "cheese", "carrot", "rice", "zucchini"];
console.log(mapArr); // Output: ["<tomato>", "<potato>", "<cheese>", "<carrot>", "<rice>", "<zucchini>"];
// The map method doesn't mutate original array. It returns new array.
// If we don't assign the returned array to a variable or print it to console,
// then we aren't doing anything with the returned array.
_______________________________________________________________________
// THESE METHODS MUTATE/MODIFY ORIGINAL ARRAY.
arr.push("pepper");
console.log(arr); 
// Output: [ 'tomato', 'potato', 'cheese', 'carrot', 'rice', 'zucchini', 'pepper' ]
// Array is mutated 

let x = arr.pop();
console.log(arr);
// Output: [ 'tomato', 'potato', 'cheese', 'carrot', 'rice', 'zucchini' ]
console.log(x); // Output: "pepper"
// Array is mutated and the last element is removed from the array.
// The pop method also returns the popped element.
// Instead of let x = arr.pop(), if we simply did arr.pop() that will work 
// as well. Just that the popped element will be gone unless assigned
// to a variable.

arr.reverse();
console.log(arr);
// Output: [ 'zucchini', 'rice', 'carrot', 'cheese', 'potato', 'tomato' ]
// Array is mutated and reversed.

arr.sort();
console.log(arr);
// Output: [ 'carrot', 'cheese', 'potato', 'rice', 'tomato', 'zucchini' ]
// Array is mutated and sorted.

arr.fill("Gone!!!");
console.log(arr);
// Output: [ 'Gone!!!', 'Gone!!!', 'Gone!!!', 'Gone!!!', 'Gone!!!', 'Gone!!!' ]
// Array is mutated and all elements replaced.
1 Like

Sorry didnt see your reply but this has been so incredibly helpful thank you.

1 Like