Finding text from a paragraph - Javascript?
I wrote the below code to find my name from a paragraph,
/*jshint multistr:true */
text = "Blah blah blah blah blah blah Elic \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";
var myName = "Eric";
var hits = [];
var l=0;
var count=0;
// Look for "E" in the text
for(var i = 0; i < text.length; i++) {
if (text[i] === "E") {
for(var j = i; j < (myName.length + i); j++) {
for(var k =1; k < 4; k++) {
if(text[i+k] === myName[k]){
hits.push(text[j]);
}
else {
break;
}
}
}
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}
But the way out goes like
[ 'E',
'E',
'E',
'r',
'r',
'r',
'i',
'i',
'i',
'c',
'c',
'c',
'E',
'E',
'E',
'r',
'r',
'r',
'i',
'i',
'i',
'c',
'c',
'c',
'E',
'E',
'E',
'r',
'r',
'r',
'i',
'i',
'i',
'c',
'c',
'c' ]
I'm pretty sure this is because of the 3rd cycle loop. Please give some suggestion for fine tuning this code so that the result is like this
[ 'E', 'r', 'i', 'c', 'E', 'r', 'i', 'c', 'E', 'r', 'i', 'c', 'E', 'r', 'i', 'c' ]
Thanks in advance.
source to share
1
var res = text.match(new RegExp(myName, 'g')).join('').split('');
2
var text = "Blah blah blah blah blah blah Elic \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";
var myName = "Eric";
var hits = [];
var l = 0;
var count = 0;
var foundName = '';
for (var i = 0; i < text.length; i++) {
foundName = '';
if (text[i] === "E") {
for (var j = 0; j < myName.length; j++) {
foundName += text[i + j];
}
if (foundName === myName) {
for (var k = 0; k < myName.length; k++) {
hits.push(foundName[k]);
}
}
}
}
console.log(hits);
source to share
You don't need both inner loops for
. What happens when you are break
, you get out of the innermost loop, but still start the second loop.
Try this instead of two inner loops: (replace the for loop that includes j
)
for (var k = 0; k < 4; k++) {
if (text[i + k] === myName[k]) {
hits.push(text[i + k]);
} else {
break;
}
}
source to share
you can try using javascript function . indexOf () .
text = "Blah blah blah blah blah blah Elic \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";
if(text.indexOf('Eric') < 0 ){
console.log("Your name wasn't found!");
}
source to share
Not sure what you want to do with the result, but assuming you just want to check if the name is in the string, you use a method .indexOf()
to:
var n = text.indexOf("Eric");
Then the result will n
display the first character index position, and if it cannot find a matching word, it will return -1
.
So you add this to a simple test like:
if(text.indexOf("Eric") <0){
\\doStuff
}
source to share
// Look for "E" in the text
for(var i = 0; i < text.length; i++) {
if (text[i] === myName[0]) {
var extracted = text.substring(i, i + myName.length);
if (myName === extracted) {
for (var j = 0; j < extracted.length; j++) {
hits.push(extracted[j]);
}
}
}
}
Scrolls down until it finds the first char of your name, extract and then press.
Fiddle: http://jsfiddle.net/pm0xqrpq/
source to share
Please try this.
text = "Blah blah blah blah blah blah Elic lah blah blah Eric blah blah Eric blah blah blah blah blah blah blah Eric";
var charFound=0;
var myName = ['E' , 'r', 'i' ,'c'];
var hits = [];
var l=0;
for(var i = 0; i < text.length; i++)
{
if(text[i] === myName[charFound])
{
charFound++;
}
else
{
continue;
charFound=0;
}
if(charFound == 4)
{
for(var j = 0; j < 4; j++)
{
hits.push(text[i-j]);
}
charFound = 0;
}
}
// Rearranging the hits counter to match the requirement
for(var k = 0; k <hits.length/2; k++ )
{
var temp="";
temp = hits[k];
hits[k]= hits[hits.length-k-1];
hits[hits.length-k-1] = temp;
}
alert(hits);
source to share