Skip to content

Instantly share code, notes, and snippets.

@lanceDamage
Created October 12, 2017 22:33
Show Gist options
  • Save lanceDamage/28b45b1db7efd5582f36f08f30e3ad1d to your computer and use it in GitHub Desktop.
Save lanceDamage/28b45b1db7efd5582f36f08f30e3ad1d to your computer and use it in GitHub Desktop.
firstProgFound
<html>
<head>
<title>Simple Page</title>
</head>
<body>
<h1 id="mainHeading"> Interesting Headline</h1>
<p>This is a VERY simple html page</p>
<!--
<script type="text/javascript" src=“script.js”></script>
This source line wasn't working on my desktop, and it doesn't seem to needed here in codepen.io. I fear something may not be working here. -->
</body>
</html>
//1
var name = prompt("What is your name?")
alert("Hello, " + name);
//2
var a;
a = 5
a = 1000000
a = 123.456
a = -500
alert(a);
//3
var phrase = "This is a simple phrase.";
alert(phrase.length);
//4
var name = prompt("What is \nyour name?");
var message = "Hello,";
alert(message + name);
//conditionals, chapter 4
var a = 5;
var b = 10;
if (a < b){
alert("Yes a is less than b");
}
if (a == b){
alert("Yes, a is equal to b");
}
//else statements
var balance = 5000
if (balance >= 0) {
alert("Yes, you have money!");
if (balance >= 10000) {
alert("The balance is large!")
}
} else {
alert("Looks like you're out of money!");
}
//strict equality, using ===
var a = 123;
var b = "123";
//equality check
if (a == b){
alert("They equal!");
} else {
alert("They are not equal!");
}
//shows equality. js is being flexible here
//if I make the above ===, different result
if (a === b){
alert("They equal!");
} else {
alert("They are not equal!");
}
//for the above, !== strictly not equal also works.
//switches
var grade = "Premium";
switch ( grade ) {
case "Regular":
alert("It's $3.15");
break; //add this break here to prevent fall through reading of the rest of the switch
case "Premium":
alert("It's $3.35");
break;
case "Diesel":
alert("It's $3.47");
break;
default:
alert("That's not a valid grade");
}
//time to take a look at functions
function myFunction(){
var a = 5;
var b = 10;
var c = 20;
var d = a + b + c;
alert("The value of d is: " + d);
}
myFunction()
// parameterize functions
//parameterize a and b
function addTwoNumbers(a,b) {
var result = a + b;
alert(result);
}
//when calling, function expects values for the a and b buckets.
//we can create variables within, or, we can give values when we call the function.
addTwoNumbers(5,10);
//check this out
function addTwoNumbers(a,b) {
var result = a + b;
return result; // return produces the value and jumps you right out of block
}
addTwoNumbers(5,10);
//now, let's store multiple iterations of the function call
//...but, this works when you call the function multiple times, and you want to store multiple values
function addTwoNumbers(a,b) {
var result = a + b;
return result;
}
var x = addTwoNumbers(5,10);
alert(x);
var y = addTwoNumbers(10,20);
alert(y);
var z = addTwoNumbers(100,200);
alert(z);
//writing an iterative loop
var amount = 0;
//create the index
var i = 1;
//check condition
while ( i < 10 ) {
amount = amount + 100;
//iterate index
i++;
}
alert("The value is " + amount);
//regular expressions
//Next we need to test our regex object.
//var myRE = new RegExp("hello");
//var myString = "Does this sentence have the word hello in it?";
//if ( myRE.test(MyString) ) //
//alert("Yes");
//
//right now we return a string, Yes, but regex has other methods to return more complex objects
//!!!!I can't seem to get the above to work. Wierd
var myArray = [10,20,"test","TRUE",99]
var i = 0;
while ( i < myArray.length ) {
///...
///...
alert("The value is:" + myArray[i]);
i++;
}
//another example of iterating through an array
var myArray = [500,500,500,500,500]
var total = 0
for ( var i = 0; i < myArray.length; i++ ) {
//add the durrent element to the total
total = total + myArray[i];
}
//after we're done with the loop
alert("The total is: " + total);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment