-1

I have to create a program that can take a sentence such as

my arms are red, my legs are red and I am red

and output the sentence with the indices of the first occurrence of the word in the sentence, so the desired output of that sentence would be

12341634910114 

I have no idea how I would do this.

Thanks

Jan Molak
  • 4,426
  • 2
  • 36
  • 32
Bobjit
  • 13
  • 3

4 Answers4

1

Here is the idea:

  1. You have to break sentence into words. The break points could be SPACE, COMMA, NEW LINE etc etc

  2. You may create 2 arrays. The first one containing all the words and the second one containing only unique words.

  3. Run both the arrays through loop comparing them against first occurrence of a word and save its index

Bubba Yakoza
  • 749
  • 2
  • 9
  • 17
0

Here's an approach.

  • I assumed you do not want punctuation, so I removed them.
  • Then I created a map with key=word and value=first occurrence.
  • After that I go through the sentence and replace every word with the index.

var text = "my arms are red, my legs are red and i am red";
// remove punctuation
text = text.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");

// split text
var words = text.split(" ");

// create map of word and first index
var m = new Map();
var i = 1;
words.forEach(function(word) {
  // first occurrence?
  if (m.get(word) == undefined) {
    m.set(word, i++);
  }
});

// replace words with index
var l = [];
words.forEach(function(word) {
  l.push(m.get(word));
});

console.log(l.join(""));

There are surely shorter ways to do this, but I think this is easier to read and understand

ppasler
  • 3,579
  • 5
  • 31
  • 51
0

You have made a mistake in your question. If a word is repeated, the index should be filled. So the index should be 12341634910114 (123416349,10,11,4)

var str="my arms are red, my legs are red and i am red";
var words=str.replace(",","").split(" ");
var occur=[];
var pos=[];
//console.log(words.length);
for(var i=0; i< words.length; i++){
  if(occur.length==0){
    occur.push([words[i],i]);
    pos.push(pos.length+1);
   }
   else{
     for(var j=0; j<occur.length; j++){
       if(words[i]==occur[j][0]){
         pos.push(occur[j][1]+1);
         break;
       }
     }
     if(j>=occur.length){ // the loop is not broken
       occur.push([words[i],i]);
       pos.push(i+1);
     }
   }
}
console.log(pos.join(""));
//console.log(occur);
//console.log(occur[1][0]);
Sagar V
  • 12,158
  • 7
  • 41
  • 68
0

Split at word boundaries .match(/\b(\w+)\b/g)

Use Array.map to map each word to number of occurrence.

Find number of occurrence by slicing array only upto the current index using Array.slice and finding number of occurrence using a Array.filter on that sliced array.

The same thing can be done in a lot different ways, using simple loops for example.

var result = "my arms are red, my legs are red and i am red?".match(/\b(\w+)\b/g).map(function(currentValue, index, array){
  return array.slice(0, index + 1).filter(x => x === currentValue).length;
}).join("");

console.log("result", result);
sabithpocker
  • 15,274
  • 1
  • 42
  • 75