简体   繁体   中英

how do I replace a word in an array?

I'm trying to practice some JavaScript, and I ran into a problem which is why the splice method is working not quite how I want it to work, so here's the challenge that I went for:

so I should write a function that reverses all the words in a sentence that start with a particular letter.

example:

specialReverse("word searches are super fun", "s")
➞ "word sehcraes are repus fun"

actually I understood the exercice and I started solving it but the problem is I knew the words that should be reversed and I reversed them as you see here:

 function specialReverse(s, c) { var words = s.split(" "); words.findIndex(i => { if (i[0] === c) { var newWords = i.split("").reverse().join(""); words.splice(i[0] === c, 1, newWords, i) } }) return words } console.log( specialReverse("word searches are super fun", "s") )

but my problem that I couldn't replace the previous words (searches and super) I think my problem is with splice because the result when I run that code is this: 在此处输入图像描述

I don't know why the word "searches" get replicated several time I think my problem is coming from the splice() method

Try this. You can use String.prototype.startsWith() ,rather than going with findIndex

 //specialReverse("word searches are super fun", "s") //"word sehcraes are repus fun" function reverse(sentence, letter) { let _ar = sentence.split(/(\s+)/); return _ar.map(ele => { if (ele.startsWith(letter)) { ele = ele.split("").reverse().join(""); } return ele; }).join(""); } console.log(reverse("word searches are super fun", "s"))

So in your question when you find the character starts with the character that you have passed through the function you need to replace the existing array word.

.splice(startingIndex, numDeletions, replacement1, replacement2, ... )

correction

words.splice(i[0] === c, 1, newWords, i) the first parameter should be the index of the word that you have found the string where it starts with passed character, here its a boolean value.

So you can use the second argument of findIndex(item, index) and get the index. See the below updated snippet.

 function specialReverse(s, c) { var words = s.split(" "); words.findIndex((item, index) => { if (item[0] === c) { var newWords = item.split("").reverse().join(""); words.splice(index, 1, newWords) } }) return words.join(" ") } console.log( specialReverse("word searches are super fun", "s") )

ALTERNATE SIMPLE WAY

A simple and one liner, basically you can use map , includes mainly to get the desired result.

Logical is simple return a new reversed words with char having, join them. and if the word has char that passed reverse it else return the word

Check the below snippet

 const specialReverse = (str, char) => str.split(" ").map(word => word.startsWith(char)? word.split("").reverse().join(""): word ).join(" ") console.log(specialReverse("words searches are super fun", "s"))

you can split the words and then look for the words to reverse

function reverseWord(word) {
  var reversed = "";
  for (var i = word.length - 1; i >=0; --i) {
    reversed += word.charAt(i);
  }
  return reversed;
}

function specialReverse(words, ch) {
   return words.split(/\s+/).map(function(word) {
      return word.startsWith(ch) ? reverseWord(word) : word;
   }).join(" ");
}

console.log(specialReverse("word searches are super fun", "s"));

This works:

 function specialReverse(s, c) { let words = s.split(" "); words.find((element, index) => { if (element[0] === c) { let newWords = element.split("").reverse().join(""); words[index] = newWords; //Replaces the value of that particular index } }) return words } let x = specialReverse("word searches are super fun", "s") console.log(x)

Code

You can try something like:

 const reverseWord = word => word.split("").reverse().join("") const reverseIfChar = (sentence, c) => sentence.split(" ").map(word => word.startsWith(c)? reverseWord(word): word).join(" ") console.log(reverseIfChar("abc dfg 010", 'd'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM