In JavaScript While Loops can execute a block of code as long as a specified condition is true. So as long as your condition is true the block of code will keep looping until your condition is false. They syntax looks like this...
while(condition)
{
block of code to be looped
}
This is very simple, now to get a more complex take on while loops, this will look something like this...
var x = 4;
while( x == 4)
{
alert("loop")
x = 5
}
This code will run the alert statement once, because I added in the x = 5, which makes the condition false and will stop the loop. Its very important to have a way for the condition to become false or else it will create an infinite loop and crash your browser.You can also do something like this...
while (i<5) { x=x + "The number is " + i + "<br>"; i++; }
This code will spit out 5 different alerts by using the "++" statement which just increase the value of a variable by 1. This is the basics of how while loops work in JavaScript.
I will be showing you multiple tutorials of basic programming for beginners in multiple languages
Thursday, December 19, 2013
Wednesday, December 18, 2013
JavaScript Tutorial 5 - Math
In JavaScript there are several math functions, they vary in diffifculty starting from very simple addition to some complex generating numbers. The very simple stuff looks like this syntax wise...
var add = 7 + 9
var subtract = 10 - 5
var divide = 10/2
var multiply = 5 * 5
var x = 5
var y = 7
x > y = false
x<y = true
This is as simple as it gets math wise. Now we will move on to generating numbers between 0 and 1. To do this we will use a Math random statement which looks like this...
var num = Math.random()
var num = 0.983782646377272
The number it spits out can be anywhere from 0 to 1. Now if we wanted to to make this out of a hundred for convenience, then all you would do is multiply this by 100 like so...
var num = Math.random() * 100
var num = 98.3782646377272
Now we are getting somewhere but do we really want that whole number with a ton of digits? well this is where the math round code comes in handy and it works just like this....
var num = Math.random() * 100
var round = Math.round(num)
var round = 98
Now we have a application that will spit out a random number between 1 and 100. Now how can this be used in an application? well I will show you a possible situation for its use...
var num = Math.random() * 100
var round = Math.round(num)
if (round >= 50)
{
alert(" im smaller then 50")
}
else
{
alert(" im greater then 50")
}
This is just a simple app that will trigger an alert statement based of the number generated. This is the basic concept of math in JavaScript.
var add = 7 + 9
var subtract = 10 - 5
var divide = 10/2
var multiply = 5 * 5
var x = 5
var y = 7
x > y = false
x<y = true
This is as simple as it gets math wise. Now we will move on to generating numbers between 0 and 1. To do this we will use a Math random statement which looks like this...
var num = Math.random()
var num = 0.983782646377272
The number it spits out can be anywhere from 0 to 1. Now if we wanted to to make this out of a hundred for convenience, then all you would do is multiply this by 100 like so...
var num = Math.random() * 100
var num = 98.3782646377272
Now we are getting somewhere but do we really want that whole number with a ton of digits? well this is where the math round code comes in handy and it works just like this....
var num = Math.random() * 100
var round = Math.round(num)
var round = 98
Now we have a application that will spit out a random number between 1 and 100. Now how can this be used in an application? well I will show you a possible situation for its use...
var num = Math.random() * 100
var round = Math.round(num)
if (round >= 50)
{
alert(" im smaller then 50")
}
else
{
alert(" im greater then 50")
}
This is just a simple app that will trigger an alert statement based of the number generated. This is the basic concept of math in JavaScript.
Tuesday, December 17, 2013
JavaScript Tutorial 4 - Arrays
An array is a special variable, which can hold more than one value at a time.
If you have a list of items instead of creating multiple var statements you can just use an array to store all the data in one line of code. For example...
Var names = [Nick,Joe,Bryan];
Ok so now we have this piece of data, but how do we access it you ask? Well each piece of data has a corresponding index number where it can be accessed from. Nick = 0 , Joe = 1 and
Bryan = 2 if you had more data you would just keep counting up by 1. So now to see how this looks in JavaScript...
var Myname = names[0]
Myname = Nick
Myname = Joe
var Myname = names[2]
Myname = Bryan
Now you can also change the data in the array by using a statement like this...
names[0] = "Luke"
That line of code just changed the result of the previous code that pulled the first peice of data in the array so now if we run the code...
var Myname = names[0]
Myname = Luke
We see that Nick changed to Luke due are previous code. This is a brief overview of Arrays in JavaScript.
JavaScript Tutorial 3 - Functions
A function is a block of code that will be executed when "someone" calls it. A function is stated by saying function followed by the name of the function for example...
function name ()
The name of this function is called name, now the syntax of a function looks like this...
function myFunction()
{alert("Hello World!");}
myFunction()
Now every time you call that function it will run that block of code this is very useful when you need to write out the same code multiple times.
At this point you are probably wondering why after the function there is "()", well this is where you can add in arguments. Basically this allows you to add in variables to your function for example...
function myFunction(name,job)
{
alert("Welcome " + name + ", the " + job);}
myFunction(name,job)
This is the basics of Functions and what there syntax is.
function name ()
The name of this function is called name, now the syntax of a function looks like this...
function myFunction()
{alert("Hello World!");}
myFunction()
Now every time you call that function it will run that block of code this is very useful when you need to write out the same code multiple times.
At this point you are probably wondering why after the function there is "()", well this is where you can add in arguments. Basically this allows you to add in variables to your function for example...
function myFunction(name,job)
{
alert("Welcome " + name + ", the " + job);}
myFunction(name,job)
This is the basics of Functions and what there syntax is.
JavaScript Tutorial 2 - If Else Statements
In JavaScript if else statements play a huge part of the language the basic syntax for them looks like this..
if (Statement)
{
Do something
}
else
{
Do something else
}
Now to apply this in a normal situation you could have some user input and say if the user says "yes" then do a command and if they say "no" do something else. This application would look like this...
var q = prompt("Is your name Nick?");
if ( q == "yes")
{
alert("Hi Nick");
}
else
{
alert("oh ok"
}
The way the alert part works is similar to prompt except it doesn't have user input, it just displays a message. You must always use two "=" signs when comparing two different types of data (variable and string).
This is it for if else statements in JavaScript.
if (Statement)
{
Do something
}
else
{
Do something else
}
Now to apply this in a normal situation you could have some user input and say if the user says "yes" then do a command and if they say "no" do something else. This application would look like this...
var q = prompt("Is your name Nick?");
if ( q == "yes")
{
alert("Hi Nick");
}
else
{
alert("oh ok"
}
The way the alert part works is similar to prompt except it doesn't have user input, it just displays a message. You must always use two "=" signs when comparing two different types of data (variable and string).
This is it for if else statements in JavaScript.
Philips Hue Applescript Controller
Philips Hue Applescript Controller
By Nick Hallowell
Step 1: Download App here: http://www.mediafire.com/?q9qav4str3r441l
Step 2:
- Pick a username that you'll use to connect to your Hue device, and make a MD5 hash of it. You can either google it or type
md5 -s *your username*in a terminal. Save this hash for the next steps. - Install resty or a similar tool to make HTTP requests. I'll let you go check out the instructions on that page, but it amounts to downloading resty with
curland then sourcing it withsource restyso that you can use it. - Press the link button on your Hue bridge.
- Make a POST HTTP request to http://*YourHueHubIP*/api with the data:
{"username": "YourMD5Hash", "devicetype": "SiriProxyHue"}. Using resty I first set the base URL to this address withresty http://10.0.1.16/apiandPOST '{"username": "YourMD5Hash", "devicetype": "SiriProxyHue"}'to send the request. You should get a response like{"success":{"username":"YourMD5HashFromBefore"}} - Store this MD5 hash and Ip Address somewhere you can access it easily.
Step 3: Open the app you downloaded and hit configure.
Step 4: First type in your Ip Address then the MD5 Hash you generated.
Step 5: Enjoy
Please Comment and let me know how it works, or if you have any problems or suggestions.
JavaScript Tutorial 1 - Variables
To start off learning JavaScript the first thing to learn is what is a variable and how they are used. Variables are stated by using the "var" statement followed by the variable name. For example...
Var x = 20;
Variables can display three different types of data here is a list of all three...
var string = "text";
var boolean = true;
var float = 56.43;
Note whenever you use a string variable the text has to be enclosed by quotation marks.This is the basic syntax for Variables.
It can get more complicated though, you can set a variable to another variable like this...
var x = 78;
var y = x;
y = 78
Simple right? You can also set a variable to a user response using the prompt statement, it can be used like this...
var Q = prompt("What is your name?");
Now Q is equivalent to whatever the user types in.This should give you a basic idea of how variables work in JavaScript.
Variables can display three different types of data here is a list of all three...
var string = "text";
var boolean = true;
var float = 56.43;
Note whenever you use a string variable the text has to be enclosed by quotation marks.This is the basic syntax for Variables.
It can get more complicated though, you can set a variable to another variable like this...
var x = 78;
var y = x;
y = 78
Simple right? You can also set a variable to a user response using the prompt statement, it can be used like this...
var Q = prompt("What is your name?");
Now Q is equivalent to whatever the user types in.This should give you a basic idea of how variables work in JavaScript.
Subscribe to:
Posts (Atom)
