# Ten Simple Javascript Exercises

Show 40 post(s) from this thread on one page
Page 1 of 4 123 ... Last
• 06-05-2009, 02:10 PM
afernandez323
Ten Simple Javascript Exercises
I need help with questions for these TEN SIMPLE JAVASCRIPT EXERCISES !!!!!!!

1.

Define a function max() that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in Javascript.

Solution
2.

Define a function maxOfThree() that takes three numbers as arguments and returns the largest of them.

Solution
3.

Write a function that takes a character (i.e. a string of length 1) and returns true if it is a vowel, false otherwise.

Solution
4.

Write a function translate() that will translate a text into "rövarspråket". That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon".

Solution
5.

Define a function sum() and a function multiply() that sums and multiplies (respectively) all the numbers in an array of numbers. For example, sum([1,2,3,4]) should return 10, and multiply([1,2,3,4]) should return 24.

Solution
6.

Define a function reverse() that computes the reversal of a string. For example, reverse("jag testar") should return the string "ratset gaj".

Solution
7.
Write a function to sort a list of words (an array) in alphabetical order

Solution

8.

Write a function findLongestWord() that takes an array of words and returns the length of the longest one.

Solution
9.

Write a function filterLongWords() that takes an array of words and an integer i and returns the array of words that are longer than i.

Solution
10.

Write a function charFreq() that takes a string and builds a frequency listing of the characters contained in it. Represent the frequency listing as a Javascript object. Try it with something like charFreq("abbabcbdbabdbdbabababcbcbab").

Can somebody help me out please I'm stuck, I can't seen to figure it out please thank you
• 06-05-2009, 02:13 PM
criterion9
Is this for school? If so try checking your book for some clues.
• 06-05-2009, 02:17 PM
afernandez323
naw i just want to know because i want to learn stuff
• 06-05-2009, 02:27 PM
David Harrison
Quote:

Originally Posted by afernandez323
1.

Define a function max() that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in Javascript.

Is Math.max(x,y) not good enough for you then?

Quote:

Originally Posted by afernandez323
2.

Define a function maxOfThree() that takes three numbers as arguments and returns the largest of them.

Code:

`Math.max(Math.max(x,y),z);`
Why don't you try having a go at these first, learn by doing.
• 06-05-2009, 02:42 PM
Y_Less
D&#233;j&#224; vu?

http://www.webdeveloper.com/forum/sh...d.php?t=209719

• 06-05-2009, 04:00 PM
Jeff Mott
The really sad part is the OP seems to actually have a great course and great teacher. (Based on this page: http://www.ling.gu.se/~lager/kurser/webtechnology/).

Good Web dev teachers are very rare.
• 06-05-2009, 09:31 PM
afernandez323
i Need all the help i can get please, Im stuck
• 06-05-2009, 09:35 PM
Four Staples
Quote:

Originally Posted by afernandez323
i Need all the help i can get please, Im stuck

Your class is going to get a LOT harder than this, I suggest you work on these yourself until you get them.

All of the material required to answer those questions is likely covered in 1-2 pages of your text.
• 06-08-2009, 03:03 PM
ricem84
Exercise 1
I'm a newbie to javascript, and I thought it was a good idea for me to try these exercises. Can someone tell me what I'm doing wrong in number 1?

Code:

```<html> <head></head> <body> <form name="myForm"> <input type="text" size="3" name="num1" /> <input type="text" size="3" name="num2" /> <input type="button" onClick="max()"> </form> <script language="javascript"> var firstNum = num1; var secNum = num2; var output; function max() {         if (firstNum > secNum)                 output = firstNum;         else                 output = secNum;         document.write(output); } </script> </body> </html>```
Undefinied is written by document.write(output). I'm assuming there's something stupid I'm missing.
• 06-08-2009, 03:36 PM
peachskittle
Hi ricem84, I'm pretty new too... so I may or may not be right or wrong at any time...

But it doesn't look like you're giving your max function the values it needs to do anything?

EDIT: OP, that class looks like it would be very useful... I'd love to take that. Maybe if you posts some of your in progress answers, we can better help you get to the right answers.
• 06-08-2009, 03:42 PM
ricem84
Huh. Good point. For some reason I thought everything was available to the function, even though that's not how it is in C++. I'm looking up passing variables now...

Okay, this works. But in Mozilla it keeps on being busy forever, which I guess means there's some flaw in the code that I'm not aware of. I didn't test for equality, but that's because I don't care. : n P

Code:

```<html> <head></head> <body> <form name="myForm"> <input type="text" size="3" name="num1" /> <input type="text" size="3" name="num2" /> <input type="button" onClick="max()"> </form> <script language="javascript"> function max() {         var output;         var firstNum = document.myForm.num1.value;         var secNum = document.myForm.num2.value;         if (firstNum > secNum)                 output = firstNum;         else                 output = secNum;         document.write(output); } </script> </body> </html>```
• 06-08-2009, 04:14 PM
David Harrison
The firstNum and secNum variables are available to the function, but you have made several errors in the code.

First off, the text box references, you can't simply treat their names as a variable like you have done, something more like this is required:
Code:

`var firstNum = document.myForm.num1.value;`
Or this:
Code:

`var secNum = document.forms['myForm'].elements['num2'].value;`
Or one of the several other ways it's possible to access form elements.

However, since the page has already loaded by the time the button is hit, even with the correct references to the text boxes, the firstNum and secondNum variables contain the values the text boxes had when the page loaded, which is an empty string, so you should move those references inside the function to get the current values.

Thirdly, you are treating strings as numbers. JavaScript is pretty forgiving with this sort of stuff, but as a matter of good practice, you really should convert them to one of the number data types first. If you only wish to check integers you can use the parseInt function, but if you wish to check decimals too, you can use the parseFloat function.

Finally, because the page has finished loading, you cannot use document.write() since that will replace the entire source code of the page. You can only use that before the page has finished loading. For an easy way to display the result you could just use an alert(), or have a third text box to put the answer into, or you could use innerHTML or similar to actually write the result to the page as I'm sure you intended document.write() to do.

Incidentally, the actual challenge never required any of this, it only states that a function take two arguments (yours has none) and returns the larger (your function returns nothing).
Code:

```function max(a,b){   if( a>b ){     return a;   }else{     return b;   } }```
The above would be called like so:
Code:

`alert("Which is larger, 27 or 13?\nAh, "+max(27,13)+" of course!");`
• 06-08-2009, 04:27 PM
JMRKER
Once you understand solution #1, solution for #2 just expands on solution for #1.
Code:

```function maxOfThree(x,y,z) {   var w = max(x,y);   return max(w,z); } // .... later .... alert("Which is larger: 27, 42 or 13?\nAh, "+maxOfThree(27,42,13)+" of course!");```
• 06-08-2009, 04:33 PM
David Harrison
Here's a slightly more challenging one, how about a function that can take one argument which is an array of numbers and return the largest.
• 06-08-2009, 05:43 PM
ricem84
I fixed things. I will try the array way tomorrow or later today. Good point about me not following directions. : n P I did feel though that if I could not find a way to input numbers it was something I should learn or at least figure out.

I am having a problem with a website I'm building that when I button is clicked starts a video of a fortune cookie being opened and showing a fortune. The fortune is a string chosen from a switch text statement and shown over the fortune video's fortune, and I'm having trouble trying to place it on top of the fortune in the fortune cookie video. I am using Javascript, and document.write() doesn't work at all.

Code:

```<html> <head> <script language="javascript"> function max() {         var output;         var firstNum = parseFloat(document.myForm.num1.value);         var secNum = parseFloat(document.myForm.num2.value);         if (firstNum > secNum)                 output = firstNum;         else                 output = secNum;         alert(output); } </script> </head> <body> <form name="myForm"> <input type="text" size="3" name="num1" /> <input type="text" size="3" name="num2" /> <input type="button" onClick="max()"> </form> </body> </html>```
Show 40 post(s) from this thread on one page
Page 1 of 4 123 ... Last

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.10938 seconds
• Memory Usage 2,419KB
• Queries Executed 11 (?)
Template Usage (24):
• (9)bbcode_code_printable
• (3)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)pagenav
• (1)pagenav_curpage
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (43):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• friendlyurl_resolve_class
• friendlyurl_clean_fragment
• friendlyurl_geturl
• pagenav_page
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates