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();
1

5 Answers 5

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)
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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();

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.