www.webdeveloper.com
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 56

Thread: Ten Simple Javascript Exercises

Hybrid View

  1. #1
    Join Date
    Jun 2009
    Posts
    3

    Exclamation 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. #2
    Join Date
    Jan 2009
    Posts
    3,346
    Is this for school? If so try checking your book for some clues.

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

  4. #4
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,276
    Quote Originally Posted by afernandez323 View Post
    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 View Post
    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.
    Every fight is a food fight when you’re a cannibal.

  5. #5
    Join Date
    May 2009
    Posts
    150
    Déjà 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. #6
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    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.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

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

  8. #8
    Join Date
    Jun 2009
    Posts
    111
    Quote Originally Posted by afernandez323 View Post
    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. #9
    Join Date
    Jun 2009
    Posts
    23

    Question 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. #10
    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 04:38 PM.

  11. #11
    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 04:58 PM.

  12. #12
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415

    Lightbulb

    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. #13
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,276
    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.
    Every fight is a food fight when you’re a cannibal.

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

  15. #15
    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>
    <head>
    
    <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];
    	}	
    	alert(largest);
    }
    
    function join()
    {
    	numbers[i] = parseFloat(document.myForm.num1.value);
    	i++;	
    	
    }
    
    </script>
    
    </head>
    <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>

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
  •  
HTML5 Development Center



Recent Articles