Change character in word

Viewed 87

I have a task to change string input into something new with array, cause my tutor said so. For example

Input  : Soda Hello
Output : * od *   * ell *

This is my code :

var r1 = require('readline-sync')
var str = r1.question('Input : ')
var output = '';

var array = str.split(' ')

for(var i=0;i<array.length;i++){

    for(var l=0;l<array[i].length;l++){
        if(l!=0 && l!=(array[i].length-1)){
            output+= array[i][l]
        }
        else{
            output += '*'
        }
    }
    output += ' '
}
console.log(output)

Is there any way to change first character in the first word without turn it again as array? Because when I try this code

for(var i=0;i<array.length;i++){
    for(var l=0;l<array[i].length;l++){
        if(l!=0 && l!=(array[i].length-1)){
            array[i][l]='*'
        }
}

It doesn't create any change. Should I turn the words into array first or there's another way?

4 Answers

You are trying to modify the string objects that are present in arr, which isn't possible. Javascript will allow you to treat a string as an array for reading characters, but attempts to write characters are ignored, and as a result the strings do not change.

Here is an alternative approach using slice:

var str = "Soda Hello"
var array = str.split(' ')

for (var i = 0; i < array.length; i++) {
  array[i] = "*" + array[i].slice(1, -1) + "*";
}

var output = array.join(" ");
console.log(output)

Or even shorter, using slice and map:

var str = "Soda Hello"
var output = str.split(' ')
    .map(word => "*" + word.slice(1, -1) + "*")
    .join(" ")
console.log(output)

As I understood you would like to replace the first and last character with an "*" and you have to use arrays because your tutor said that.

So you can use the following

let s = "Sodaa Helloo";

let arr = s.split(" ");

let resultArr = arr.map((elem) => {
   let temp1 = elem.replace(elem.charAt(0), "*");
   let temp2 = temp1.slice(0, temp1.length - 1);
   temp2 += "*";

return temp2;
})

console.log(resultArr)

  1. split the str by ' ' to make words
  2. for each word, convert to chars and replace first and last chars
  3. use join to make word (and again join to form a str)

const addStar = (str) => {
  return str
    .split(" ")
    .map((chars) =>
      [...chars]
        .map((char, i) => ([0, chars.length - 1].includes(i) ? "*" : char))
        .join("")
    )
    .join(" ");
};

const str = "Soda Hello";

console.log(addStar(str));

According to your question, you want to replace the first and last character of your strings with "*". You can do it easily -

  1. Remove the first and last characters using substring()
  2. Use interpolation to add "*" to beginning and end of the string.

Below snippet explains it -

var words = ["Hello", "World"];
var result = words.map((element) => { return`*${element.substring(1, element.length-1)}*` });

console.log(result);

But as your tutor has asked you to use arrays, you can change the approach like this -

var words = ["Hello", "World"];

for(let i = 0; i<words.length; i++){
  let inner = words[i].split("");
  for(let j = 0; j<inner.length; j++){
    if(j == 0 || j==inner.length-1){
      inner[j] = "*";
    }
  }
  words[i]=inner.join("");
}

console.log(words);

Related