Reputation: 35
What I want this code to do is:
search for the position of all the words present in the sentence "some warriors know when they are defeated,
whether some of them are too stubborn to accept it and they
are the one who die " inside the array_of_words
NOTE: I simply mean to search for the position of all the words present in array split_Sentence
inside the array array_of_words
.
I have given an example in the code below in blue color after //
.
<html>
<body>
<script>
const Sentence= "some warriors know when they are defeated,
whether some of them are too stubborn to accept it and they
are the one who die "
const split_Sentence = Sentence.split(" ")
var array_of_words = ["some", "warriors", "know", "they",
"defeated", "whether", "too", "stubborn", "one", "die"]
var arrayLength=split_Sentence.length
//for example a function that searches for the position of the word "some" from the first array i.e. split_Sentence in the second array i.e. array_of_words
</script>
</body>
</html>
Upvotes: 0
Views: 140
Reputation: 41
/*
What i want this code to do is:
search for
the position of all the words present in the sentence "some warriors know when they are defeated, whether some of them are too stubborn to accept it and they are the one who die "
inside the array_of_words
NOTE: I simply mean to search
for the position of all the words present in array array_from_sentence inside the array array_of_words.
I have given an example in the code below in blue colour after //.
*/
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
/*
( method ) Array < string[] > .shift(): string[]
Removes the first element from an array and returns it.If the array is empty, undefined is returned and the array is not modified.
method ) Array < string > .filter( predicate: ( value: string, index: number, array: string[] ) => unknown, thisArg ? : any ): string[]( +1 overload )
Returns the elements of an array that meet the condition specified in a callback
function.
//------------------------------------------------------------------------------------------
@param predicate— A
function that accepts up to three arguments.The filter method calls the predicate
function one time
for each element in the array.
@param thisArg— An object to which the this keyword can refer in the predicate
function.If thisArg is omitted, undefined is used as the this value.
*/
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
const sentence =
'some warriors know when they are defeated whether some of them are too stubborn to accept it and they are the one who die';
const array_from_sentence = sentence.split(' ');
console.log('array_from_sentence: ', array_from_sentence);
/*
array_from_sentence: ['some', 'warriors', 'know','when', 'they', 'are',
'defeated', 'whether', 'some','of', 'them', 'are',
'too', 'stubborn', 'to','accept', 'it', 'and',
'they', 'are', 'the','one', 'who', 'die'];
*/
let array_of_words = [
'some',
'warriors',
'know',
'they',
'defeated',
'whether',
'too',
'stubborn',
'one',
'die',
];
//for example a function that searches for the position of the word "some" from the first array i.e. array_from_sentence in the array-of-words i.e. array_of_words
let ArrOfSentenceAndWords = [array_from_sentence, array_of_words];
let intersectionOfArrays = ArrOfSentenceAndWords.shift().filter(function (v) {
return ArrOfSentenceAndWords.every(function (a) {
return a.indexOf(v) !== -1;
});
});
console.log('intersectionOfArrays: ', intersectionOfArrays);
/*
intersectionOfArrays: [
'some', 'warriors', 'know', 'they',
'defeated', 'whether', 'some', 'too',
'stubborn', 'they', 'one', 'die']
*/
function finder(word) {
if (intersectionOfArrays.includes(word)) {
return `The word '${word}' was found at position ${array_of_words.indexOf(
word
)} of the array "array_of_words"`;
} else {
return `the word did not exist in the array_of_words`;
}
}
finder('some');
console.log(finder('some'));
Upvotes: 1
Reputation: 143
You can use sentence_split.indexOf for each str in array_of_words
<html>
<body>
<script>
const sentence= "some warriors know when they are defeated, whether some of them are too stubborn to accept it and they are the one who die "
const split_sentence = sentence.split(" ")
var array_of_words = ["some", "warriors", "know", "they", "defeated", "whether", "too", "stubborn", "one", "die"]
split_sentence.forEach(function(w){
console.log(w, " index ", array_of_words.indexOf(w));
});
</script>
</body>
</html>
Upvotes: 1
Reputation: 68933
You can loop through the first array and in each iteration you can find the index of the current word from the second array using Array.prototype.indexOf():
split_Sentence.forEach(function(w){
console.log(array_of_words.indexOf(w));
});
Demo:
const Sentence= `some warriors know when they are defeated,
whether some of them are too stubborn to accept it and they
are the one who die `;
const split_Sentence = Sentence.split(" ");
var array_of_words = ["some", "warriors", "know", "they",
"defeated", "whether", "too", "stubborn", "one", "die"];
var arrayLength=split_Sentence.length;
//for example a function that searches for the position of the word "some" from the first array i.e. split_Sentence in the second array i.e. array_of_words
split_Sentence.forEach(function(w){
console.log(array_of_words.indexOf(w));
});
Upvotes: 2