So me and bro found out today that our chapter on Javascript sucks

thecoolnessrune

Diamond Member
Jun 8, 2005
9,673
583
126
Right now we are taking a full course on C#. No big deal we're doing just fine there.

But on the Web Programming side, we're doing a chapter on javascript.. And nothing here is making sense.

We need some help understanding this. So we're asking for homework help. If you're willing to help, please explain some of the syntax behind this. We're trying, and have went about 7 hours today on it, but its just not really clicking for us. I'm sure eventually it will, but I think we need more help then just a chapter in a book.

Question 1:

Parameter is an array of names, represented as strings.

We need to return the number of names in the array that end in either "ie" or "y".

So first we made an array:

var namesArray = new Array("Taffy", "Mary", "Daphnie",
"Todd", "Barbie", "Carl");

Next we sorted it out into one long string:

var name_string = namesArray.sort();

I'm guessing what we need to do now is run a search on it to the effect of:

name_string.search("y$"||OR"ie$")

then i'm guessing its something like:

var sum=0,
count;
for (count =0; count > -1; count++)
sum += count;

Then finish with:

document.write("There are ", + sum + "names ending in ie or y");

I really feel like its not that simple. And it's not since its not running. But the suggestions here to be very simple. This is my first few days in javascript and obviously I know nothing advanced of the language. I just need to know whats gone wrong and what i have to do to make it right. The book isn't helping much. As seen by bro's post today, our book didn't even tell us what an alert box is limited to.

anyone wanna give a hand?

 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
why not just loop through the array and have a conditional checking to see if the value ends in 'ie' or 'y'. if it does, add 1 to the counter
 

thecoolnessrune

Diamond Member
Jun 8, 2005
9,673
583
126
Originally posted by: troytime
why not just loop through the array and have a conditional checking to see if the value ends in 'ie' or 'y'. if it does, add 1 to the counter

Can you tell me how I would loop through the array? I'm so new at this and its hardly making any sense to me..
 

Woosta

Platinum Member
Mar 23, 2008
2,978
0
71
Just out of curiosity - how old are you and what book are you using? 98% of the ECMAScript code ( including snippets posted on this forum ) are old, out of fashion, deprecated, logically imperfect. I would suggest a book by John Resig, Crockford, Flanagan, or the E262 manual itself.

Oh as for your code..

Explanation:

This declares a variable in the current namespace ( global ) assigned to an array literal which is a unique object passed by reference in ECMAScript. A simple for loop with the initial declaration of three variables in the same scope as names, 'i' , 'regex', and 'counter' which are assigned to an integer from the length of the array object, an assignment to a regex literal ( searching for 'b', 'a', end of line ), and another reference to an integer (0). The loop starts at the last and goes to the first, which is less work internally on the interpreter than a normal "i=0;i<foo;i++" loop, if the condition is true then counter is incremented by one, and the result of this is 1, because only one of the four string literals in the array ends with 'ba'.

This is why I didn't go to college and just learned from either gurus or manuals, because professors and books are crappy, old, deprecated.

var names = ['John', 'Cassy', 'Alba', 'Mark'];

for ( var i = names.length, regex = /ba$/, counter=0; i--; ) {
if ( regex.test( names[ i ] ) ) {
counter++;
}
}

counter;

Ugh, ^ newline characters/tabs aren't preserved.. my code looks fugly :(

var name_string = namesArray.sort();


I'm guessing what we need to do now is run a search on it to the effect of:

name_string.search("y$"||OR"ie$")

Array.prototype.sort is a method of arrays. It can accept a function as a parameter but usually you just use it as is, and it will sort them out I believe by converting the first character to its ordinal. The return value is an array, NOT A STRING and you assumed it was a string because you tried to use the String.prototype.search method, which should throw an error in the interpreter stating that the function does not have a search method.
 

thecoolnessrune

Diamond Member
Jun 8, 2005
9,673
583
126
Originally posted by: Woosta
Just out of curiosity - how old are you and what book are you using? 98% of the ECMAScript code ( including snippets posted on this forum ) are old, out of fashion, deprecated, logically imperfect. I would suggest a book by John Resig, Crockford, Flanagan, or the E262 manual itself.

Oh as for your code..

var names = ['John', 'Cassy', 'Alba', 'Mark'];

for ( var i = names.length, regex = /ba$/, counter=0; i--; ) {
if ( regex.test( names ) ) {
counter++;
}
}

counter;


Ugh, ^ newline characters/tabs aren't preserved.. my code looks fugly :(


I'm 19. I'm using Web Programming by Sebesta and its not making a lick of sense. I got C# easily, so I don't know why I can't understand this stuff for a a hill of beans..

like what you wrote. I see you made an array..

And you're testing for the prescence of "ba" at the end of the name right?

what does the "if" statement mean when broken down? What does the stand for next to names? And why in the above line do you use i--? And why is .length invoked?

You see how stupid I feel right now? :(

To put icing on the cake, once counter is established, how would I have it display? Would a simple:

document.write("The letters appear" + counter + times.");

??

In the regex area, could I use:

regex=/ba$/||OR/rk$/

To search for both results? Or does the OR operator not work where I'd have to somehow create another loop and have it add further to the counter? Gah, my mind feels so numb even I'm having trouble knowing what I'm tlaking about.
 

Woosta

Platinum Member
Mar 23, 2008
2,978
0
71
Ugh, because I chose 'i' as the counter the forum makes text italicized and the i is not preserved, lol.

It means that if the regular expression pattern is found, in other words if 'ba' are the last 2 chars then it evaluates to true. You can use regex = /ba$|rk$/.

Try this.

var names = ['John', 'Cassy', 'Alba', 'Mark'];

for ( var counter = names.length, regex = /ba$|rk$/, total=0; counter--; ) {
if ( regex.test( names[ counter ] ) ) {
total++
}}

total;

In a regular expression I believe you cannot use the keyword OR, it's just one vertical pipe ( as opposed to two vertical pipes in most cases outside of regular expressions ).

I suggest you ditch that book immediately, as I've never heard of it ( and I pretty much have skimmed all JS books out there ).
 

Woosta

Platinum Member
Mar 23, 2008
2,978
0
71
Oh, to answer your other question.. document.write() should never be used in a real ( non demo ) page. If you're testing and using a modern browser such as Opera, Safari, or Firefox you usually have a console handy and you can type your scripts in there ( no need to alert as it gives you the return value back ). It took me about 40 or so seconds to whip that out using Firebug's console, no saving of files or anything. If you're inside an HTML page ( served as text/html as *real* xhtml should not support document.write() ) you could either use console.log if you have Firebug, or alert() or you're in something like IE6 and you didn't create your own console or aren't using a plugin/add-on.

As for embedding strings/numbers on web pages, you should use standard dom methods such as .. var span = document.createElement('span'); span.appendChild( document.createTextNode('string') ); document.body.appendChild( span );. If you're lazy, create the span element and use innerHTML ( though its proprietary, it can still be useful at times ). Or you can follow the trend and grab a framework such as jQuery and just do $('el').text('foo') which basically abstracts hundreds of lines of code for you to type it out in one line.

If you're testing and learning, I guess you could get away with using document.write(). You could do something like..

function w( string ) { document.write( w ) ; } // for ease of use

But this should not be used on a real page.
 

thecoolnessrune

Diamond Member
Jun 8, 2005
9,673
583
126
Originally posted by: Woosta
Oh, to answer your other question.. document.write() should never be used in a real ( non demo ) page. If you're testing and using a modern browser such as Opera, Safari, or Firefox you usually have a console handy and you can type your scripts in there ( no need to alert as it gives you the return value back ). It took me about 40 or so seconds to whip that out using Firebug's console, no saving of files or anything. If you're inside an HTML page ( served as text/html as *real* xhtml should not support document.write() ) you could either use console.log if you have Firebug, or alert() or you're in something like IE6 and you didn't create your own console or aren't using a plugin/add-on.

As for embedding strings/numbers on web pages, you should use standard dom methods such as .. var span = document.createElement('span'); span.appendChild( document.createTextNode('string') ); document.body.appendChild( span );. If you're lazy, create the span element and use innerHTML ( though its proprietary, it can still be useful at times ). Or you can follow the trend and grab a framework such as jQuery and just do $('el').text('foo') which basically abstracts hundreds of lines of code for you to type it out in one line.

If you're testing and learning, I guess you could get away with using document.write(). You could do something like..

function w( string ) { document.write( w ) ; } // for ease of use

But this should not be used on a real page.

It wont be used on a real page. The problem is right now document.write is *all* I know.. The boook hasn't taught anything else and to be honest I don't have a single clue about anything you said in your second paragraph x_x I'm still trying to hold on and fully understand what you said in your above post..

This really bites tonight :p
 

Woosta

Platinum Member
Mar 23, 2008
2,978
0
71
It's ok. I started learning programming in general and javascript like 2 years ago in HS and it was hard as hell to grasp at first, moving on to bigger languages like Python now, ECMAScript isn't really moving because MS has a monopoly on browsers and their JSCRIPT engine is like.. 5-8 years behind on things.
 

thecoolnessrune

Diamond Member
Jun 8, 2005
9,673
583
126
Well that one is complete now! I think I understand all this just a little more now.. just a little x_x..

One down.. 3 to go.. Better than starting with 8, 9 hours ago..
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
i'd be interested in seeing the performance difference between using regex and string comparison

i know what the answer is for php, but i've never done the benchmark in javascript
 

Woosta

Platinum Member
Mar 23, 2008
2,978
0
71
i'd be interested in seeing the performance difference between using regex and string comparison

i know what the answer is for php, but i've never done the benchmark in javascript
It would only make sense that string operations are faster, regex is almost always more work.. no matter what language. However if he went with string operations, he would have to make a custom method. For now, he wants to find only the characters that end with something... if later on he decides he wants to instead find characters that start with.. or contain.. he would have to update his string method over and over again, that or update his itty bitty regex, which is why a regex would be better to use.