Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

JavaScript / jQuery to Analyze Sentence

I've recently began learning JavaScript and while out of my current scope I want to start considering a feature that will most likely give me the most trouble in the project I hope to complete.

Basically what I'm hoping to accomplish is to take a sentence and convert it into a question and an answer, or term & definition. For example:

"an increase in government purchases is an appropriate discretionary fiscal policy if equilibrium real GDP falls down potential real GDP"

would be turned into:

Question: "an increase in ______ is an appropriate discretionary fiscal policy if equilibrium real GDP falls down potential real GDP"

Answer: "government purchases"

What are some possible solution to make this happen?

3 Answers

Javascript has a lot of nice built in functions. I'm not entirely sure I know exactly what you are asking without knowing how you want to accomplish it, but I can guarantee you that you should be able to accomplish sentence manipulation with regular expressions. An easier solution would be using built in string manipulation functions like split(). http://www.w3schools.com/jsref/jsref_split.asp this has some handy examples that should clear things up.

What i'm hoping to accomplish is to have a feature where a user, most likely a student, can take notes and then convert the notes they took automatically into quizzes to study off of.

That's a nice project. Depending on how in depth you want to go, you may want to try using keywords. If classes haven't changed since I was there, they usually have a list of important keywords and terms you need to know along with a list of definitions. you can store these in an external file or internal list/array that stores these keywords. the program can read the list of these special words and use any of a variety of different ways to match them in a sentence. it can match sentences using period and question mark. It can extract them and then format it and output it to a file as a test page, or you can create an interactive test using loops. The tools you'll need to accomplish this are dependent on how you decide to implement. You can use arrays, regular expressions, string matching, file io. a simple example is

var str = "Visit Microsoft!";
 var res = str.replace("Microsoft", "W3Schools");
//The result of res will be:
Visit W3Schools!