Ten Simple Javascript Exercises

afernandez323

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

criterion9

Is this for school? If so try checking your book for some clues.

afernandez323

naw i just want to know because i want to learn stuff

David_Harrison

afernandez323;1011622 wrote:

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.[/quote]Is [url=http://www.w3schools.com/jsref/jsref_max.asp]Math.max(x,y)[/url] not good enough for you then?

afernandez323;1011622 wrote:

2.

Define a function maxOfThree() that takes three numbers as arguments and returns the largest of them.[/quote][code]Math.max(Math.max(x,y),z);[/code]

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.

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

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:

var firstNum = document.myForm.num1.value;

Or this:

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

function max(a,b){
if( a>b ){
return a;
}else{
return b;
}
}

The above would be called like so:

alert("Which is larger, 27 or 13?\nAh, "+max(27,13)+" of course!");

JMRKER

Once you understand solution #1, solution for #2 just expands on solution for #1.

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.

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.