.Slice array doesn't work, instead of subtracting some values it just copies the array as it
Here is my little script for finding a word in text. Part of the problem is in the loop when slicing the array doesn't work. Array.slice wants to take on values (0,6) and some words of the iterated array are shorter, so I guess this might be a problem. Could you give me some advice to deal with such a situation?
/*jshint multistr:true */
var text = "Etiam accumsan facilisis blandit. Praesent convallis sapien at sagittis ultrices. Proin adipiscing Michal, dolor at adipiscing volutpat, tellus neque adipiscing risus, at interdum magna ante quis risus. Quisque semper in mi imperdiet interdum. Fusce laoreet leo blandit, convallis tellus ut, posuere mauris michal. Quisque eu pulvinar felis. Maecenas ultricies a nulla et dignissim. Praesent egestas quam massa, eget dignissim nulla fringilla nec. Etiam non nulla imperdiet, tempus erat sit amet, luctus nibh. Aenean sit amet elementum nunc. Duis venenatis lacinia rutrum. Fusce vulputate michal lacinia odio, non laoreet metus placerat id. Nam ac risus et nisl pretium vulputate.";
var myName = "michal";
var hits = [];
var name = myName.toLowerCase();
var text2 = text.toLowerCase();
// changes string into array by space
var textWords = text2.split(" ");
// looks for each array value and compares it with the name
for (e = 0;e <= textWords.length; e++) {
console.log("______ new iteration -> another word to slice _______");
// each time 'e' changes, it turns array into another one, consisting of just a word (+ space or comma or dot)
var g = e+1;
var textWord = textWords.slice(e, g);
console.log(textWord);
// !! PROBLEM part, JS will not slice the former array so it is just name.length long !!
var potentialName = textWord.slice(0,name.length);
console.log(potentialName);
// pushes the result word into empty array 'hits'
if (potentialName === name) {
hits.push(potentialName);
// console.log(hits);
}
}
// takes the first value of the result array
var nameCopy = hits.slice(0,1);
// counts the number of result values
var count = hits.length;
// checks if ther is any matching value and how many of them there is
if ((nameCopy === name) && (count > 1)) {
console.log("I've found your name in the text! It there " + count + " times.");
} else if (nameCopy === name) {
console.log("I've found your name in the text once.");
} else {
console.log("Your name is not in the text.");
}
source to share
slice
returns an array, which means that:
-
potentialName
is an array, not a string, and therefore will never equal (===
) beforename
. -
textWord
is also an array, and since it isg
alwayse + 1
, we can conclude that it is always an array containing one element, which is the current word. I repeat:textWord
is an array containing the word, not the word itself, as you seem to expect.
Run your program step by step, starting from the first line of the loop, on the first iteration:
-
e
equals 0. -
g
e + 1
which is equal0 + 1
to which is 1. -
textWord
textWords.slice(e, g)
which['etiam', 'accumsan', ...].slice(0, 1)
, which is['etiam']
(array). -
potentialName
textWord.slice(0, name.length)
which['etiam'].slice(0, 6)
, which['etiam']
.
I think the third step is not what you want. You probably want to get the e-word. What you need to do is not slice
as well []
as: textWords[e]
. Using this, the first iteration textWord
will have 'etiam'
instead ['etiam']
. Then you can safely compare textWord
with name
and see if they are equal with ===
. The lines do not need to be the same length.
Hope this helps.
Of course, these are easier ways of counting words in the text, but I assume this is a learning exercise, so it is good for this purpose.
source to share