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


so, i'm working on exercise questions. code seems working fine until decide loop through string replace instance of period nothing. reason, loop doesn't work. imagine has not calling somehow, i'm not sure how call loop. thought loops automatically overwrote looping through. here exercise , incomplete solution:

write javascript function parameterize string.

function string_parameterize(string) {      var lowercase_string = string.tolowercase();      var split_string = lowercase_string.split(" ");      var joined_string = split_string.join("-");      (i = 0; < joined_string.length; i++) {          if (joined_string[i] === ".") {              joined_string[i] === "";          }      }      return joined_string;  }    //test data :  console.log(string_parameterize("robin singh usa."));

the answer should robin-singh-from-usa without period, keeps coming out robin-singh-from-usa. period.

the other answers not taking account strings in javascript immutable. can not change individual characters in string. build new string.

in javascript, strings immutable. trying change characters in string not work:

function string_parameterize(string) {      var lowercase_string = string.tolowercase();      var split_string = lowercase_string.split(" ");      var joined_string = split_string.join("-");      (i = 0; < joined_string.length; i++) {          if (joined_string[i] === ".") {              joined_string[i] = "";          }      }      return joined_string;  }    //test data :      console.log(string_parameterize("robin singh usa.")); //this not work:

you can build new string using for loop individually add each character not . newstring:

function string_parameterize(string) {      var lowercase_string = string.tolowercase();      var split_string = lowercase_string.split(" ");      var joined_string = split_string.join("-");      var newstring = '';      (i = 0; < joined_string.length; i++) {          if (joined_string[i] !== ".") {              newstring += joined_string[i];          } //we replacing '.' nothing, '', no need else      }      return newstring;  }    //test data :  console.log(string_parameterize("robin singh usa."));

regular expressions

this would, however, done regular expressions, .replace() method:

function string_parameterize(string) {      var lowercase_string = string.tolowercase();      var newstring = lowercase_string.replace(/ /g,'-'); //replace spaces '-'.      var newstring = newstring.replace(/\./g,''); //replace '.' nothing. '.'                                                   //  has quoted \.                                                   //  has special meaning in regexp.           return newstring;  }  //test data :  console.log(string_parameterize("robin singh usa."));

which can done in 1 statement:

function string_parameterize(string) {      return string.tolowercase().replace(/ /g,'-').replace(/\./g,'');  }  //test data :  console.log(string_parameterize("robin singh usa."));


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -