Thread: Ten Simple Javascript Exercises

1. Registered User
Join Date
Jun 2009
Posts
3

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

2. MPM, PMP, CSM, BS CIS
Join Date
Jan 2009
Location
Ohio
Posts
3,352
Is this for school? If so try checking your book for some clues.

3. Registered User
Join Date
Jun 2009
Posts
3
naw i just want to know because i want to learn stuff

4. 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?

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.

5. Registered User
Join Date
May 2009
Posts
150
D&#233;j&#224; vu?

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

Some solutions were already offered in what I can only assume is your other thread from your other username, to which you didn't respond.

6. 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.

7. Registered User
Join Date
Jun 2009
Posts
3
i Need all the help i can get please, Im stuck

8. 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.

9. Registered User
Join Date
Jun 2009
Posts
23

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.

10. Registered User
Join Date
Jun 2009
Posts
112
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.
Last edited by peachskittle; 06-08-2009 at 03:38 PM.

11. Registered User
Join Date
Jun 2009
Posts
23
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>```
Last edited by ricem84; 06-08-2009 at 03:58 PM.

12. 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!");`

13. 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!");```

14. 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.

15. Registered User
Join Date
Jun 2009
Posts
23
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.

http://webdeveloper.com/forum/showthread.php?t=210673

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>```

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•