Andrew Andrew - 1 month ago 6
Javascript Question

Changing individual characters in a string does not work in a loop

So, I'm working on some exercise questions. My code seems to be working fine up until I decide to loop through the string to replace any instance of a period with nothing. For some reason, the loop doesn't work. I imagine it has something to do with not calling it somehow, but I'm not sure how to call the loop. I thought that loops automatically overwrote what they are looping through. Here is the exercise and my incomplete solution:

Write a JavaScript function to parameterize a string.



function string_parameterize(string) {
var lowercase_string = string.toLowerCase();
var split_string = lowercase_string.split(" ");
var joined_string = split_string.join("-");
for (i = 0; i < joined_string.length; i++) {
if (joined_string[i] === ".") {
joined_string[i] === "";
}
}
return joined_string;
}

//Test Data :
console.log(string_parameterize("Robin Singh from USA."));





The answer should be
robin-singh-from-usa
without the period, but it keeps coming out as
robin-singh-from-usa.
with the period.

Answer

The other answers are not taking into account that strings in JavaScript are immutable. You can not change individual characters in a string. You build a new string.

In JavaScript, strings are immutable. Trying to change the characters in a string does not work:

function string_parameterize(string) {
    var lowercase_string = string.toLowerCase();
    var split_string = lowercase_string.split(" ");
    var joined_string = split_string.join("-");
    for (i = 0; i < joined_string.length; i++) {
        if (joined_string[i] === ".") {
            joined_string[i] = "";
        }
    }
    return joined_string;
}

//Test Data :    
console.log(string_parameterize("Robin Singh from USA.")); //This will not work:

You can build a new string using your for loop to individually add each character that is not a . to the newString:

function string_parameterize(string) {
    var lowercase_string = string.toLowerCase();
    var split_string = lowercase_string.split(" ");
    var joined_string = split_string.join("-");
    var newString = '';
    for (i = 0; i < joined_string.length; i++) {
        if (joined_string[i] !== ".") {
            newString += joined_string[i];
        } //We are replacing '.' with nothing, '', so no need for an else
    }
    return newString;
}

//Test Data :
console.log(string_parameterize("Robin Singh from USA."));

Regular Expressions

This would, however, normally be done with Regular Expressions, specifically the .replace() method:

function string_parameterize(string) {
    var lowercase_string = string.toLowerCase();
    var newString = lowercase_string.replace(/ /g,'-'); //Replace all spaces with '-'.
    var newString = newString.replace(/\./g,''); //Replace all '.' with nothing. The '.'
                                                 //  has to be quoted with \. as it
                                                 //  has special meaning in a RegExp.     
    return newString;
}
//Test Data :
console.log(string_parameterize("Robin Singh from USA."));

Which can be done all in one statement:

function string_parameterize(string) {
    return string.toLowerCase().replace(/ /g,'-').replace(/\./g,'');
}
//Test Data :
console.log(string_parameterize("Robin Singh from USA."));