Skip to content

Instantly share code, notes, and snippets.

@bmakarand2009
Created September 13, 2012 18:30
Show Gist options
  • Save bmakarand2009/3716490 to your computer and use it in GitHub Desktop.
Save bmakarand2009/3716490 to your computer and use it in GitHub Desktop.
Javascript-samples
//Rolls a 6 faced dice 6000 times and summarizes the results of
//how many times each face came
var f1 = 0;
var f2 = 0;
var f3 = 0;
var f4 = 0;
var f5 = 0;
var f6 = 0;
var diceFace;
console.time("fdice");
var i=0;
console.log("starting the loop");
while( i< 6000){
i++;
diceFace = Math.floor(1 + Math.random()*6);
switch(diceFace){
case 1:
++f1;
break;
case 2:
++f2;
break;
case 3:
++f3;
break;
case 4:
++f4;
break;
case 5:
++f5;
break;
case 6:
++f6;
break;
}
}//end of while
console.timeEnd("fdice");
console.log("f1 :" + f1);
console.log("f2 :" + f2);
console.log("f3 :" + f3);
console.log("f4 :" + f4);
console.log("f5 :" + f5);
console.log("f6 :" + f6);
//Display a greeting based on time
//http://jsfiddle.net/mbhatamb/uK9qb/
var result = "Good Morning";
console.log("result");
var now = new Date();
var curHours = now.getHours();
console.log(curHours);
if(curHours >12 && curHours < 18 ){
result = "Good Afternoon";
}else if(curHours >18){
result = "Good Evening";
}
console.log(result);
document.write("<h1> Its is " + result +"</h1>");
//Addition of Two Numbers
//http://jsfiddle.net/mbhatamb/Yht3s/1/
console.time("mystart");
var firstNum = window.prompt("enter the first num");
var num1;
num1 = parseInt(firstNum);
var num2 = parseInt("50");
var sum = num1+num2;
console.log(sum);
console.timeEnd("mystart");
document.write("<h1> Sum is " + sum "</h1>");
//find an average of 4 student scores
//http://jsfiddle.net/mbhatamb/sAUTM/
var scoreCounter = 1;
var noOfPeople = 4;
var totalScore=0;
while(scoreCounter <= noOfPeople){
var personScore = window.prompt("Enter the score for person" + scoreCounter);
var personScoreInt = parseInt(personScore);
totalScore = totalScore + personScoreInt;
scoreCounter++;
}
console.log("totalScore is" + totalScore);
var avgScore = totalScore / noOfPeople;
document.write("<h1> Average score of a person is" + avgScore +"</h1>");
//find an Max of 3 numbers
//http://jsfiddle.net/mbhatamb/4Rfd3/
var num1 = 10;
var num2 = 13;
var num3 = 13.1
var maxVal = maximum(num1,num2,num3);
console.debug("max val is" + maxVal);
function maximum(n1,n2,n3){
return Math.max(n1,Math.max(n2,n3));
}
//concept of arrays
//Javascript arrays can store different objects
//http://jsfiddle.net/mbhatamb/QBgGh/
//Simple Array
var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";
//Other way to define array, also this is a mixed array of difft types
var mycars = new Array("bmw",500);
var myArray = new Array();
//Sparsely populated array
var myArray = new Array();
myArray[0] = 200;
myArray[5] = 300;
console.log(myArray[0]); //shows 200
console.log(myArray[2]);//shwos undefined
console (myArray.length); //this will show 6
//Associative Arrays
var studentTypes = new Array();
studentTypes["NAME"] = "TEXT";
studentTypes["MARKS"] = "NUMBER";
studentTypes["DOB"] = "DATE";
alert("Type of MARKS is :" +
studentTypes["MARKS"]
//functions and scopes
//<script type="text/javascript">
var y =1;
function init() {
console.log("value of y is" + y); //output is 1
var y = 16;
console.log("value of y after local declaration is" + y); //output is 16
}
//</scrip>
//fibonacci series
//store the fibonacci series in an array
var myArray = new Array;
var maxNum = 100;
var a = 0;
var b = 0;
var c = 1;
while(c < maxNum)
{
myArray[myArray.length] = c;
a = b;
b = c;
c = a + b;
}
// displays
// Fibonacci Series : 1,1,2,3,5,8,13,21,34,55,89
console.log("Fibonacci Series :" + myArray);
// display - Array Length : 11
console.log("Array Length :" + myArray.length);
@wetstevs
Copy link

Oops

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment