JMRKER's Profile User Rating: -----

Reputation: 131 Stalwart
Group:
Contributors
Active Posts:
849 (0.37 per day)
Joined:
25-October 08
Profile Views:
10,207
Last Active:
User is offline Today, 06:23 PM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Who Cares
Favorite Browser:
FireFox
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
50

Latest Visitors

Icon   JMRKER has not set their status

Posts I've Made

  1. In Topic: ~~n symbol question

    Posted 13 Feb 2015

    View PostNallnaom, on 13 February 2015 - 10:52 AM, said:

    Bitwise logical operator huh! I'm sorry your example does not really give me much help, but I know what to read on about now! So thanks! :bigsmile:/>/>



    var isSquare = function(n){
      n = Math.sqrt(n);
      return ~~n === n;
    }
    
    

    Bottom line in the code above:
    It checks each bit of the answer of the Math.sqrt() function
    to make sure that it is an EXACT match.

    If it is not an exact match, then the value has probably been rounded somewhere.

    ~ is NOT
    ~~ is NOT NOT

    Logically, NOT NOT 'something' should be equal to the original 'something'.


    Function used in an example:
    <script type="text/javascript">
    
    var isSquare = function(n){
      n = Math.sqrt(n);
      return ~~n === n;
    }
    
    var x = 25;  alert(x+' is square of '+Math.sqrt(x)+ ' and is a perfect square: '+isSquare(x));
        x = 20;  alert(x+' is square of '+Math.sqrt(x)+ ' and is a perfect square: '+isSquare(x));
    
    </script>
    
    
  2. In Topic: ~~n symbol question

    Posted 12 Feb 2015

    Bitwise logical operator

    See: http://www.w3schools...comparisons.asp

    Operator 	Description 	Example 	Same as 	Result 	Decimal
    & 	AND 	x = 5 & 1 	0101 & 0001 	0001 	1
    | 	OR 	x = 5 | 1 	0101 | 0001 	0101 	5
    ~ 	NOT 	x = ~ 5 	 ~0101 	1010 	10
    ^ 	XOR 	x = 5 ^ 1 	0101 ^ 0001 	0100 	4
    << 	Left shift 	x = 5 << 1 	0101 << 1 	1010 	10
    >> 	Right shift 	x = 5 >> 1 	0101 >> 1 	0010 	2
    
    Note 	The examples above uses 4 bits unsigned examples. But Javascript uses 32-bit signed numbers.
    
    Because of this, in Javascript, ~ 5 will not return 10. It will return -6.
    
    ~00000000000000000000000000000101 will return 11111111111111111111111111111010
    
    



    Test script:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> HTML5 page </title>
    
    </head>
    <body>
    <script type="text/javascript">
    // ~   NOT     x = ~ 5      ~0101  1010    10
    
    var x = 5;
    var y = ~ x;
    var z = ~~ x;
    
    alert(x+' is '+x.toString(2)+'\n'+y+' is '+y.toString(2)+'\n ~~ 5 is '+z+' is '+z.toString(2));
    
    </script>
    
    </body>
    </html>
    
    
    
  3. In Topic: Is there a simpler way to do this?

    Posted 7 Feb 2015

    View Postfelgall, on 06 February 2015 - 09:56 PM, said:

    View PostJMRKER, on 07 February 2015 - 01:30 PM, said:

    Another solution to look at when you become more experienced ...


    You shouldn't use for..in with arrays - you can get unexpected results such as array methods being included along with the properties. A for..in loop should only ever be used with objects.


    OK, but how does that apply to this particular problem?
  4. In Topic: Is there a simpler way to do this?

    Posted 6 Feb 2015

    View PostAutumnBrown1094, on 06 February 2015 - 06:51 PM, said:

    It was my code! haha.

    This is an interesting way to do it, I definitely wouldn't have thought of it but it works as well! Thanks :)/>/>/>


    Another solution to look at when you become more experienced ...

    <!DOCTYPE html>
    <html>
    <head>
    <title>Autumn Brown Assignment 5</title>
    </head>
    <body>
    
    <script>
      var states = { "Washington":"Olympia", "Oregon":"Salem", "California":"Sacremento", "Montana":"Helena","Idaho":"Boise" }
      for (var state in states) { alert("The capital of " + state + " is " + states[state]); }
      var faveState = prompt ("What is your favorite state of these to visit?");
      var fnd = -1;
      for (var state in states) { if (state == faveState) {  fnd = state; } }
      if (fnd != -1) { alert("The capital of " + state + " is " + states[state]); }
                else { alert(faveState+' is an invalid entry'); }
    </script>
    
    </body>
    </html>
    
    


    For extra credit, now see if you can replace the 'prompt' and 'alert' commands.
  5. In Topic: Is there a simpler way to do this?

    Posted 5 Feb 2015

    One possibility ...

    <!DOCTYPE html>
    <html>
    <head>
    <title>Autumn Brown Assignment 5</title>
    </head>
    <body>
    
    <script>
      var states = new Array("Washington", "Oregon", "California", "Montana","Idaho");
      var capitals = new Array("Olympia", "Salem","Sacremento", "Helena","Boise");
      for(var i=0, len=states.length; i < len; i++) {
        alert("The capital of " + states[i] + " is " + capitals[i]);
      }
      var faveState = prompt ("What is your favorite state of these to visit?");
    
      var fav = states.indexOf(faveState);
      if (fav != -1) { alert("The capital of " + states[fav] + " is " + capitals[fav]); }
      else { alert(faveState+' is not a state listed'); }
    
    </script>
    
    </body>
    </html>
    
    


    Whoops! Was that YOUR code or the teachers? If it was not your solution, ignore answer!

My Information

Member Title:
D.I.C Addict
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:
Location:
Florida
Programming Languages:
javascript, perl, java, forth, basic, fortran, assembly (several)

Contact Information

E-mail:
Private
Website URL:
Website URL  http://

Friends

Showing 50 random friends of 4

Comments

JMRKER has no profile comments yet. Why not say hello?