Are function parameters are hoisted like var?

I can’t understand why str gets reversed after going on the if statement function? is str affected when it goes on isPlanidrome() function?


function makePlanindrome(str) {
    str=str.split("");
    console.log(str)//[ 'a', 'b', 'c', 'd', 'e', 'd' ]
    if (isPlaindrome(str)){
        return str.join("");
    } 
    console.log(str)//[ 'd', 'e', 'd', 'c', 'b', 'a' ]
    //some other code
    return str;
}

function isPlaindrome(input){
    return input.join("")===input.reverse().join("");
}

makePlanindrome("abcded")

This line overwrites the original value of str passed into the function. It was originally a string and now it is an array.

You passed a reference to this array to the isPlaindrome function and then call the reverse method on it in the following line:

return input.join("")===input.reverse().join("");

Arrays are passed by reference and not by value. This means, when you make changes to the input array using reverse (reverse method changes the array in place and does not create a new array), you have changed input where ever else you make use of it.

What is the best way to avoid this? Is it normal to creat new variable that copies that array’s values on isPlanidrome() function?

Not sure what you mean by “normal”. You do not even need arrays to solve this challenge. However, in general, it is best not to mutate values passed into the function. Creating a new variable for the array would be the way to go here.

1 Like

I did this to avoid splitting the str and join it later

function digitDegree(str) {
    if (isplaindrome(str)){
        return str;
    } 
    for(let i =1 ;i<str.length+1;i++){
        if(isplaindrome(str.concat([...str].slice(0,i).reverse().join("")))){
            return str.concat([...str].slice(0,i).reverse().join(""));
        }
    }
}

function isplaindrome(str){
    return str===[...str].reverse().join("");
}

do you see any improvements to this code?

What is digitDegree supposed to do? What challenge/problem does it solve?

If the goal is to return a palindrome of the original string if it is not already a palindrome, then what about:

function reverseStr(str) {
  return [...str].reverse().join('');
}

function isplaindrome(str) {
  return str === reverseStr(str);
}

function digitDegree(str) {
  return isplaindrome(str) ? str : str + reverseStr(str.slice(0, -1));
}

sorry, that was mistake
I solve problems on VSC, so I don’t change the function’s name with each challenge I solve

the challenge was to make a string palindrome if it is not
the challenge :point_down: if you are interested

https://app.codesignal.com/arcade/intro/level-10/ppZ9zSufpjyzAsSEx

You don’t really need the first if statement. Also, you can remove some of the repeated code like:

function buildPalindrome(str) {
  for(let i in str) {
    const testStr = str.concat([...str].slice(0,i).reverse().join(""));
    if (isPalindrome(testStr )){
      return testStr;
    }
  }
}

function isPalindrome(str){
  return str===[...str].reverse().join("");
}
1 Like