# Thread: Ten Simple Javascript Exercises

#### Hybrid View

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

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

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

Code:
```<html>

<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;
}

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

15. Registered User
Join Date
Jun 2009
Posts
23

## Tried Array Problem

I tried it but my join() function doesn't even work. Not sure what the problem is. Maybe tomorrow I'll be smarter. Are all variables outside functions global in Javascript?

Code:
```<html>

<script language="javascript">

var numbers = new Array();
var i = 0;

function max(var numList)
{
var largest = Number.NEGATIVE_INFINITY;
for (j=0; j < numList.length; j++)
{
if (numList[j] > largest)
largest = numList[j];
}
}

function join()
{
numbers[i] = parseFloat(document.myForm.num1.value);
i++;

}

</script>

<body>

<form name="myForm">
<input type="text" size="3" name="num1" />
<input type="button" value="AddNum" onClick="join()">
<input type="button" value="Find Max" onClick="max(numbers)">
</form>

</body>
</html>```