www.webdeveloper.com
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 56

Thread: Ten Simple Javascript Exercises

  1. #16
    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>

  2. #17
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,276
    Well that's a ... pretty unique way of generating an array of numbers. Wouldn't it have been easier to just hardcode an array into the script since the key part is the max function?

    Anyway, your code does work when you take the var out from before the argument in the function. function max(numList) not function max(var numList).
    Every fight is a food fight when you’re a cannibal.

  3. #18
    Join Date
    Apr 2009
    Posts
    96
    if i do a simple google search i can find answers to all of these in minutes

  4. #19
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,350

    Exclamation

    Quote Originally Posted by Rufe0 View Post
    if i do a simple google search i can find answers to all of these in minutes
    So ...

    And you lose all skills to create your own stuff
    when google does not supply the answer or you don't ask the right question.

  5. #20
    Join Date
    Jun 2009
    Posts
    112
    I love how the OP makes this massive copy-paste post, and then disappears, waiting to see how many answers will appear?

  6. #21
    Join Date
    Jun 2009
    Posts
    23
    True, but if he has to take a test, good luck on it. I read a whole book on Javascript and don't remember jack because I didn't try coding stuff. I'm only putting up ones I'm stuck on. You definitely have cajones, however, if you put the stuff up and don't even try to do it and let people help you. It's one thing to say your stuck and another thing to show your best effort.

    You're really not supposed to but var's? in the function ()? I think in C++ you always do that. Weird.

    Oh. And join() didn't work for me because I had document.write("the") at the top of the function to see if it was working, which was a little bit dumb. :nP.

  7. #22
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,276
    Yeah, the reason you have to do that in C++ is because you have to tell it what data type to expect. JavaScript is an interpreted language, so there are many differences, this is one. It stores all data the same way, regardless of what it is, and it works out the type from what you try to store in the variable.

    This also means you can change the data type that a variable stores too, something that can't be done with C.

    You don't _really_ have to declare variables in JavaScript, but it's considered good practice to do so. Variable declerations can have an effect though, for instance if you declare a variable inside a function, it will only be available from within that function, and once it's finished running, the variable is forgotten.
    Every fight is a food fight when you’re a cannibal.

  8. #23
    Join Date
    Jun 2009
    Posts
    23

    #5

    Huh. Maybe I'll need help on all of these.

    For number 4:

    Is a string an array?
    Can I go:

    array1 = new Array();
    array1 = "string";

    where array1[2] = r

    That doesn't seem right...how do you split "string" into characters?"

  9. #24
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,276
    For that you can use .split(), (and there's also the reverse, .join()).
    Code:
    var testStr = "testing 123";
    
    var split1 = testStr.split("");
    alert(split1[1]);	// alerts "e"
    
    var split2 = testStr.split(" ");
    alert(split2[1]);	// alerts "123"
    Once you dip your toe into regular expressions, this would also be a good one to come back to and do again using those.
    Last edited by David Harrison; 06-09-2009 at 03:55 PM.
    Every fight is a food fight when you’re a cannibal.

  10. #25
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,350
    You can 'split' if you need to, but for strings it may be easier just to use 'charAt()' function.

  11. #26
    Join Date
    Jun 2009
    Posts
    23

    #4

    I tried #4, but the prompt doesn't even work. Can having pop-ups turned off block that? The function translate() isn't even called. I put an alert in the first line of translate(), and it never showed up.

    Code:
    <html>
    <head>
    
    <script language="javascript">
    
    function findType(chara){
    var vowels=new Array();
    vowels[0]='a';
    vowels[1]='e';
    vowels[2]='i';
    vowels[3]='o';
    vowels[4]='u';
    vowels[5]='y';
    for(i=0;i<6;i++){
    if(chara==vowels[i]){
    	return true;
    else
    	return false;
    }
    }
    }
    
    translate()
    {
    	var w=prompt("Enter a word:","");
    	var newWord;
    	var k = 0;
    	word = w.split("");
    	for (var j=0; j<word.length; j++)
    	{
    		newWord[k] = word[j];
    		k++;
    		if (!findType(word[j]))
    		{
    			newWord[k] = "o";
    			k++;
    			newWord[k] = word[j];	
    			k++;
    		}			
    	}
    		
    	newWord.join();
    	alert(newWord);
    	
    }
    
    translate();
    </script>
    
    </head>
    <body>
    
    </body>
    </html>

  12. #27
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,276
    You didn't say the magic word, which in this case is function.
    Code:
    function translate(){
      ...
    }
    I see you've decided to go the route of determining a character to be a consonant if it is not a vowel (though personally I count y as a consonant). But what if the character being tested is a space, or a number, or punctuation, or anything else that a character can be?
    Every fight is a food fight when you’re a cannibal.

  13. #28
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    translate is not defined, you have not defined it via: function translate() but instead have invokations: translate(), also newWord is not defined either. Another (sort-of) issue is the letter 'y' is actually NEVER a vowel, except in particular context's. I saw another post here once where someone wrote their 'vowel' program to include 'y' as a vowel at random. That is dead wrong also, so don't do that either. Whether or not 'y' is a vowel in the current context of a word is dependent on a few things which can also be syllable specific. So don't even try to determine it in a program... unless you want to give your self a big headache... , just leave 'y' out.

  14. #29
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    In case anyone cares to know when 'y' IS a vowel, this is a good link:
    http://www.decoz.com/Y_vowel-consonant.htm

  15. #30
    Join Date
    Jun 2009
    Posts
    23
    a e i o u and sometimes y

    I can't believe how many times somebody taught me that. They just never said what the "sometimes" is. I'm just trying to figure out coding and not complete thoroughness. Maybe I'll be good enough one day to realize when I leave "function" out.

    It still doesn't work. I don't even get a pop-up prompt.

    Code:
    <html>
    <head>
    
    <script language="javascript">
    
    function findType(chara){
    var vowels=new Array();
    vowels[0]='a';
    vowels[1]='e';
    vowels[2]='i';
    vowels[3]='o';
    vowels[4]='u';
    vowels[5]='y';
    for(i=0;i<6;i++){
    if(chara==vowels[i]){
    	return true;
    else
    	return false;
    }
    }
    }
    
    function translate()
    {
    	var w=prompt("Enter a word:","");
    	var newWord;
    	var k = 0;
    	word = w.split("");
    	for (var j=0; j<word.length; j++)
    	{
    		newWord[k] = word[j];
    		k++;
    		if (!findType(word[j]))
    		{
    			newWord[k] = "o";
    			k++;
    			newWord[k] = word[j];	
    			k++;
    		}			
    	}
    		
    	newWord.join();
    	alert(newWord);
    	
    }
    
    translate();
    </script>
    
    </head>
    <body>
    
    </body>
    </html>



    I see you've decided to go the route of determining a character to be a consonant if it is not a vowel (though personally I count y as a consonant).
    But what if the character being tested is a space, or a number, or punctuation, or anything else that a character can be?

    How would you do that without listing them one by one? I could go the easy way and say that I asked for a word so none of that stuff applies.

    When you create a new array, can you add on to the end of it without defining a length for it?
    Last edited by ricem84; 06-10-2009 at 06:35 PM.

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