0

I need to do as follows:

I've got an array of strings containing last names. Some of them ends with letter 'i'.

manLastNames = ["testowski","bucz","idzikowski","gosz"];

I need to make a function which will iterate over this array of strings and if there is an element ending with 'i', I need to replace this 'i' for 'a', otherwise just leave string as it is.

At the end I want to have another array where all last 'i's are replaced with 'a's.

womanLastNames = ["testowska","bucz","idzikowska","gosz"];

This is what I have now, but Im pretty sure that it start being crap at some point

    var rep = function() {
  var manLastNames = ["testowski","bucz","idzkowski","gosz"];
  var womanLastNames = new Array(4);


  for (var i=0; i<manLastNames.length; i++) {
    var lastName = manLastNames[i];
    if (lastName.substr(lastName.length - 1, 1) == 'i') {
        lastName = lastName.substr(0, lastName.length - 1) + 'a';
  }
  }

  for (var i=0; i<womanLastNames.length; i++) {
    womanLastNames[i] = lastName[i];

  }

  console.log(womanLastNames);
}
rep();
420
  • 11
  • 1
  • 1
    Possible duplicate of [How to replace all occurrences of a string in JavaScript?](http://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – Heretic Monkey Nov 18 '16 at 15:25

5 Answers5

4

Try the code:

var manNames =  ["testowski","bucz","idzkowski","gosz"];
var womanNames = manNames.map(function(name) { 
    return name.endsWith("i") ? name.slice(0, -1) + "a" : name; 
});
console.log(womanNames)

If your interpreter supports ES6, the following is equivalent:

names.map((name)=>name.endsWith("i") ? name.slice(0, -1) + "a" : name)
Wonjung Kim
  • 1,873
  • 15
  • 18
1

Here is solution

var rep = function() {
  var manLastNames = ["testowski","bucz","idzkowski","gosz"];
  var womanLastNames =[];

  for (var i=0; i<manLastNames.length; i++) {
    var lastName = manLastNames[i];
    if (lastName.charAt(lastName.length - 1) == 'i') {
        lastName = lastName.substr(0, lastName.length - 1) + 'a';
   }
   womanLastNames.push(lastName);
  }
  console.log(womanLastNames);
}
rep();

Another solution is to use .map method like this, using a callback function:

var manLastNames = ["testowski","bucz","idzikowski","gosz"];
function mapNames(item){
   return item[item.length-1]=='i' ? item.substr(0, item.length-1) + "a" : item;
}
console.log(manLastNames.map(mapNames));
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
1

Depending on how efficient you need to be, you can use regular expressions to do both tasks:

var new_name = name.replace(/i$/, 'a');

will replace the last "i" in a string with "a" if it exists

var new_name = name.replace(/i/g, 'a');

will replace all "i"s in a string with "a".

var names = ["testowski", "bucz", "idzkowski", "gosz"];
console.log("original", names);

var last_i_replaced = names.map(function(name) {
  return name.replace(/i$/, 'a');
});
console.log("last 'i' replaced", last_i_replaced);

var all_i_replaced = names.map(function(name) {
  return name.replace(/i/g, 'a');
});
console.log("all 'i's replaced", all_i_replaced);
Briley Hooper
  • 1,271
  • 6
  • 16
0

This should work:

    var rep = function() {
  var manLastNames = ["testowski","bucz","idzkowski","gosz"];
  var womanLastNames = manLastNames;
  for(var i=0; i<manLastNames.length;i++){
    if(manLastNames[i].charAt(manLastNames[i].length-1)=='i'){
      womanLastNames[i]=manLastNames[i].substr(0,womanLastNames[i].length-1)+'a';
    }
  }
  console.log(womanLastNames);
}
rep();
Feathercrown
  • 2,547
  • 1
  • 16
  • 30
0

Here is another solution

var manLastNames = ["testowski","bucz","idzkowski","gosz"];
var womanLastNames = []
manLastNames.forEach(x => {
  if (x.charAt(x.length-1) === "i") womanLastNames.push(x.slice(0,-1).concat("a"));
  else womanLastNames.push(x);
});

console.log(womanLastNames);
kevin ternet
  • 4,514
  • 2
  • 19
  • 27