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")

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?

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