Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

Java Help with Javascript, Beginner questions D: Please come in!

views
     
TSkokhoong0624
post Sep 20 2015, 11:10 PM, updated 9y ago

Casual
***
Junior Member
404 posts

Joined: Aug 2015
QUOTE
/*jshint multistr:true */

text = "Blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";

var myName = "Eric";
var hits = [];

// Look for "E" in the text
for(var i = 0; i < text.length; i++) {
if (text[i] === "E") {
  // If we find it, add characters up to
  // the length of my name to the array
  for(var j = i; j < (myName.length + i); j++) {
  hits.push(text[j]);
  }
}
}

if (hits.length === 0) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}

^ THIS IS THE (SHOULD BE) END PRODUCT
QUOTE
/*jshint multistr:true */
var text = "Kok says hello. Kok says hi. Hello Kok Have a nice day."
var myName = "Kok"
var hits = []
for(var i = 0; i < text.length; i++){
    if (text[0]==="K"){
    }
   
}

^THIS IS MY CODE
I am learning Javascript and stuck with this for thing... It's supposed to look in a text for my name.
I don't understand the "for" command and how to push data into an array...
Can a code master here take some time... to explain briefly how this code supposed to function?

Thank you for reading this and have a nice day! smile.gif icon_question.gif icon_question.gif

bandit9956
post Sep 21 2015, 10:59 AM

Getting Started
**
Junior Member
65 posts

Joined: Nov 2008
Hi kokhoong0624,

JavaScript for loops work just like any for loop.


1) "for(var i = 0; i < text.length; i++)"

this means that it will loop through each char sequence in string variable 'text';


2) "if (text[0]==="K")"

this is wrong here, the index of text should be text[i], not text[0]. Coz if define text[0], it will always check text[0].


Lets say you want to find all the "Kok" words in the string variable 'text', the following code may guide you.

» Click to show Spoiler - click again to hide... «


TSkokhoong0624
post Sep 21 2015, 07:51 PM

Casual
***
Junior Member
404 posts

Joined: Aug 2015
QUOTE(bandit9956 @ Sep 21 2015, 10:59 AM)
Hi kokhoong0624,

JavaScript for loops work just like any for loop.
1) "for(var i = 0; i < text.length; i++)"

this means that it will loop through each char sequence in string variable 'text';
2) "if (text[0]==="K")"

this is wrong here, the index of text should be text[i], not text[0]. Coz if define text[0], it will always check text[0].
Lets say you want to find all the "Kok" words in the string variable 'text', the following code may guide you.

» Click to show Spoiler - click again to hide... «

*
THANK YOU VERY MUCH FOR TAKING SO MUCH TIME TO EXPLAIN FOR ME :')
(FINALLY FOUND MY MISTAKE T__T)
I'm so grateful!! biggrin.gif

I still have one little question.. I don't understand the push part.
Does push(text[j]) push all the text after "K" into the array..? Because the for command is ++ and how does it know when it will stop?
And if the MyName.length < 3 and my name appeared on the 6th character and i = 5
If [j] = [i] then j is 5 and how can 5 < 3 ...
I know its a stupid question and I'm quite blur with this for concept..

I'll take time to digest it again later!

Thanks again!! You made my day! thumbup.gif laugh.gif

nyem
post Sep 22 2015, 02:50 AM

Enthusiast
*****
Senior Member
749 posts

Joined: Jan 2007


QUOTE(kokhoong0624 @ Sep 21 2015, 07:51 PM)
Does push(text[j]) push all the text after "K" into the array..? Because the for command is ++ and how does it know when it will stop?
j++ will stop when j < (myName.length + i) returns false.

QUOTE(kokhoong0624 @ Sep 21 2015, 07:51 PM)
And if the MyName.length < 3 and my name appeared on the 6th character and i = 5
If  [j] = [i] then j is 5 and how can 5 < 3 ...
j < (myName.length + i)
= 5 < (3 + 5)
= 5 < 8


Not sure about the rules, but the code will match K and any 2 subsequent characters, including white spaces

Running the code against 'KOKA KOLA' will give you ['KOK',KA ','KOL']

bandit9956
post Sep 22 2015, 12:30 PM

Getting Started
**
Junior Member
65 posts

Joined: Nov 2008
Hi kokhoong0624,

QUOTE
Does push(text[j]) push all the text after "K" into the array..?


When you call push(text[j]), it pushes ONE CHARACTER, which is text[j].
It pushes 3 times (which is myName.length), thats why your name will be pushed into the new array.


QUOTE
And if the MyName.length < 3 and my name appeared on the 6th character and i = 5
If [j] = [i] then j is 5 and how can 5 < 3 ...
I know its a stupid question and I'm quite blur with this for concept..


As pointed out by nyem, the condition is

QUOTE
(myName.length + i)



If i = 5, it will become "for(var j = 5; j < (myName.length + 5); j++) ";
TSkokhoong0624
post Sep 23 2015, 09:45 AM

Casual
***
Junior Member
404 posts

Joined: Aug 2015
QUOTE(nyem @ Sep 22 2015, 02:50 AM)
j++ will stop when j < (myName.length + i) returns false.
j < (myName.length + i)
= 5 < (3 + 5)
= 5 < 8
Not sure about the rules, but the code will match K and any 2 subsequent characters, including white spaces

Running the code against 'KOKA KOLA' will give you ['KOK',KA ','KOL']
*
QUOTE(bandit9956 @ Sep 22 2015, 12:30 PM)
Hi kokhoong0624,
When you call push(text[j]), it pushes ONE CHARACTER, which is text[j].
It pushes 3 times (which is myName.length), thats why your name will be pushed into the new array.
As pointed out by nyem, the condition is
If i = 5, it will become "for(var j = 5; j < (myName.length + 5); j++) ";
*
Thank you guys again..! I finally get it............. rclxms.gif biggrin.gif biggrin.gif Didn't see the + i initially. hahaha got it already!!! biggrin.gif
This basic programming is already making me headache... rclxub.gif

anangryorc
post Sep 27 2015, 10:02 PM

On my way
****
Senior Member
597 posts

Joined: May 2006


Try jumping straight into functional programming instead (https://lodash.com/), it will make your life much easier.
nyem
post Sep 30 2015, 12:55 PM

Enthusiast
*****
Senior Member
749 posts

Joined: Jan 2007


QUOTE(anangryorc @ Sep 27 2015, 10:02 PM)
Try jumping straight into functional programming instead (https://lodash.com/), it will make your life much easier.
*
or regular expression

CODE

'Blah blah blah blah blah blah Eric blah blah blah Eric blah blah Eric blah blah blah blah blah blah blah Eric'.match(/Eric/g).length
4

"Kok says hello. Kok says hi. Hello Kok Have a nice day.".match(/Kok/g).length
3



 

Change to:
| Lo-Fi Version
0.0145sec    0.42    6 queries    GZIP Disabled
Time is now: 29th March 2024 - 01:19 PM