Skip to content

Instantly share code, notes, and snippets.

@AhmedHelalAhmed
Created January 27, 2018 21:52
Show Gist options
  • Save AhmedHelalAhmed/9de3669af97eba643c33321eb497a8b6 to your computer and use it in GitHub Desktop.
Save AhmedHelalAhmed/9de3669af97eba643c33321eb497a8b6 to your computer and use it in GitHub Desktop.
javascript summary
function print(obj)
{
console.log(obj);
}
function type(obj){
console.log(typeof(obj));
}
////for client side
///*
//
// for validity
// for interactive with user
// for alert html elements or css
// for animation
//
// */
//
///* syntax */
//
////javascript case sensitive
//
//var x = 1;
//var X = 2;
//if (x == X)
//{
// document.write("not case sensitive");
//}
//else
//{
// document.write("casesensitive");//case sensitive
//}
//
////there is some rules for varibles
///*
// * variables start with letter only and underscore
// * to define variable just put var word before it
// */
//var _x;
////var 2u;...error
//var x2;//right
///*
// * there are some reserved word like name , for , while and so on
// */
////var boolean;...error reserved word
////var name;..reserved word
//
///*
// * string with "" or ''
// */
//var string1 = "hello";
//var string2 = "world";
///*
// * if you did give value for variable it will be undefined
// */
//var _hello;
//document.write(_hello);//undefined
//
//
//
///*
// * variables and functions used camel case
// * one word all small
// * two word first small and the rest capital
// */
//
//var firstName;
//
//
//
////comments
///*comments*/
//
//
///*
// *
// * datatype is change dynamically
// */
//
//var x = 5;//..number
//var x = "new datatype";//..string
//
///*
// *
// * datatypes
// * object
// * string
// * number
// * boolean
// * null
// * undefined
// * just 6 types
// */
//
///*
// * operators
// */
///*
// * + ==> addition and concate with two lifes
// */
//var x = 5;
//var y = 1;
//var z = x + y;//addition ... numbers
//document.write(z);
//
//x = "ahmed ";
//y = "ali";
//z = x + y;//concate ... strings
//document.write(z);
//
//
//var u = 5;
//var w = "ahmed";
//var q;
//q = u + w;
//document.write(q);//string .. concate
//q = w + u;
//document.write(q);//string .. concate
//
///*
// * operators : * , / , - , ++ , -- , %
// */
//
///*
// * to show messgae alert(sms)
// */
//
//alert("hello javascript");
//alert(2);
//
///*
// * to get input prompt
// * take string showing it as message above the input box
// * always return string
// */
//
//var x = prompt("enter your name");
//alert("you write " + x);//....!
//
///*
// * prompt take and give the same datatype which it string
// * if the user enter no thing then it return null
// * i.e. nothing
// */
//document.write(null);
//alert(null);
//alert("nullx="+null);//.....!
//var a = 1;
//var b = "1";
//
//if (a == b)
//{
// alert("string=value in number value");
//}
//
//if (a === b)
//{
// alert("datatype!=datetype and value =value . . it's not true");
//}
////a=2;
//if (a !== b)
//{
// //it's act like !=
// alert("datatype!=datetype and value =value");//it's true here
//}
//a=1;
//b = 1;
//if (a === b)
//{
// alert("datatype=datetype and value =value");
//}
//
//
//
//
///*
// * condition return boolean i.e. true or false
// */
//
///*
// * falsy are 6 like datatype
// * ""
// * 0
// * NaN .....becareful
// * null
// * undefined
// * false
// */
//
//if(0||false||""||null||undefined||NaN){
// alert("false wins");
//}
//
///*
// * parseInt and parseFloat
// */
//
//var x="1.5";
//var n=parseInt(x);
//var m=parseFloat(x);
//alert(n);//1
//alert(m);//1.5
//
//
//
//var n="ahmed ali";
//alert(parseInt(n));//NaN not a number
///*
// * confirm - - - return true or false
// * it's options > > > ok cancel
// */
//
//alert(confirm("are you sure you want to close ?"));
/*
Ask the user to enter his first name .. done
Ask the user to enter his last name .. done
Welcome the user using his full nam)e (ex: Welcome Ahmed Ali) .. done
Ask the to enter his age. You must validate the user input (positive numbers only)
Show the status of the user knowing that
Child is between 1-10
Teenager is between 11-18
Grown up is between 19-50
Old is greater than 50
Bonus: use switch case instead of if else statements
*/
//var firstName=prompt("Enter your first name");
//var lastName=prompt("Enter last name");
//alert("Welcome "+firstName+" "+lastName);
//do
//{
// var age = prompt("Enter your age "); //......!
// //if i enter nothing means null it's go out of the loop!
//}
//while (age < 0);
//
//if (null < 0)
//{
// document.write("null<0");//......!
// //this give false and not enter the if statment
//}
//while (true)
//{
// var age = prompt("Enter your age ");
//
// if (age > 0)
// {
// break;
// }
//}
/*
Show the status of the user knowing that
Child is between 1-10
Teenager is between 11-18
Grown up is between 19-50
Old is greater than 50
Bonus: use switch case instead of if else statements
*/
//if(age>=1&&age<=10){
// alert("Child");
//}else if(age>=11&& age <=18){
// alert("Teenager");
//}else if(age>=19&&age<=50){
// alert("Grown");
//}else if(age>50){
// alert("Old");
//}
/*
* the idea
* every condition give true or false
* then i need the true one so i switch for true
*/
//switch (true)
//{
// case (age >= 1 && age <= 10):
// alert("Child");
// break;
// case (age >= 11 && age <= 18):
// alert("Teenager");
// break;
// case (age >= 19 && age <= 50):
// alert("Grown");
// break;
// case (age > 50):
// alert("Old");
// break;
//}
/*
*
Ask the user if he wants to check for a number
Ask the user to enter a number
Show the word "odd" if the number is odd and show "even" if the it is even
*/
//var flag = confirm("wants to check a number");
//if (flag)
//{
// var n = prompt("Enter a number");
// if (n % 2)
// {
// document.write("odd");
// }
// else if (!(n % 2))
// {
// //i note if(!n%2)...give error or work wrong
// document.write("even");
//
// }
//}
/***********end lecture 1**********/
///*
// * ternary operator
// */
//x = 0;
//var a = (x == 0) ? (1) : (0);//1
//if (a)
//{
// alert("true");
//}
/*
* arrays
* three nice ways
* zero based
*/
//var array1=[];//way1 ... empty
//var array2=[1,2,3];//way2
//alert(array2[0]);
//alert(array2[1]);
//alert(array2[2]);
//var array3=new Array();//way3 . . . empty
/*
* length of the array
* array.length
*
*/
//var a = ["A", "B", "C"];
//alert(a.length);
/*
* splice
*/
//var a = [];
//a.push("A");//1[0]
//a.push("B");//2[1]
//a.push("C");//3[2]
//a.push("D");//4[3]
//a.push("E");//5[4]
//function printArray()
//{
//console.log(a.length);
//console.log(a);
//}
//length=5
//for (var i = 0; i <= a.length; i++)
//{
// console.log(a[i]);//a[5]...give undefined
//}
//console.log("-------");
//printArray();
//console.log(a.splice(0,1));
//printArray();
/*
* splice function return array of reomved items from original array
*/
//a.splice(2,2);
//printArray();
/*
* the index with us
* the index is first bound and count from it
* i.e. it will be number 1 in count
*/
//console.log(a.splice(2,2));
/*
* if the element not exist then it will do no thing
* it take what is exist and do not cause problem if the count
* give element not exist
*/
//printArray();
//console.log(a.splice(a.length,5));//this will give empty array
//as there is no element in position a.length
//this donot give any problem
/*.............#...............*/
/*
* array functions
*/
//var arr = [1, 2, 3, 4, 5, 3, 7];
////i can access and change the value of the array directly
//arr[0]=10;
//console.log(arr);
//console.log("indexOf " + arr.indexOf(3));//2
////return the first index of the element
//console.log("lastIndexOf " + arr.lastIndexOf(3));//5
////return last index element occurr
//console.log(arr.length);//7
//console.log(arr);
//console.log("push " + arr.push(10));//8
////push return the new length of the array
////put in last like stack
//console.log(arr);
//console.log("pop " + arr.pop());//10
////pop retrun the poped element
////like stack pop from last
//console.log(arr);//before reverse
//console.log("reverse " + arr.reverse());//this will reverse the original array
//console.log(arr);//the reversed array
//
//console.log("unshift " + arr.unshift(122));
//console.log(arr);
////like push return the new length of the array
////put in top
//console.log("join " + arr.join(" "));
//console.log(arr.join());
////by default join separate the elements by ,
////it make the array as string separat each element with and thing
////you can change the , by put string in it and it will be the separator
//console.log(arr);
//
//arr2 = ['A', "B"];//will turn all element to string
//console.log(arr2);
//console.log("concat " + arr.concat(arr2));
////this will make new array you must store it variable or you will lose it
////arr and arr2 donot change from concat
//console.log(arr);
//console.log(arr2);
/*.............#...............*/
/*
* string with double or single you give it's value
*
*/
//var str="ahmed says : \"welcome to javascript\"";
////skip the character using \ the opposite of the comment sign
////\x . . . skip thois character x
//console.log(str);
//var str="hello";
//console.log(str[0]);
//str[0]="a";//this is nothing . . . do nothing . . . not give error even
//console.log(str);
////you can accesss string and array by index
////you can modify value by index in array but string you can not
//
/*
* strings functions
*/
//var str="helllooo";
//print(str);
//print(str.indexOf("ll"));//2
//print(str.lastIndexOf("ll"));//3
//print(str.indexOf("llll"));
//return -1 if the string not found
//print(str.replace("lll",""));//heooo
//print(str);//helllooo
//replace do not change the original string
//it's make new string
/*
* split function
* make array
* push the string in that array
* if it takes no parameter
*/
//var str = "hello, ahmed";//string
////str[0]="AA";//noeffect
//print(str);
////print(str);
////str.split();//string on the fly
//print(str.split());
//var x=str.split();
////x.push("AA");
////x[0]="AA";
//print(x);
//
//var y=[];
//y.push(str);
//print(y);
////split do not change in the original string
////make new array and push the string in it
//print(str);
/*
* split function with string delimeter
* make arrays equal to n+1 delimeters
* and push parts as strings to it
* to arrays
* n delimeter ... n+1 arrays
* 1 delimeter ... 2 arrays
*
*/
////split the string into parts
////push each part in array
//var str="hello, ahmed";//string
//print(str);
//str.split(",");//array of strings
//print(str.split(","));
//var str="Ahmed";
//var arr=str.split();
//arr.push("ali");
//print(arr[0]);
//var strfromarray=arr[0];
//print(arr.join(","));
//print(str);
/********************/
/*
*
* replace function
* not change in the original string
* it return the new string changed
*/
//var str="ahmed";
//print(str.replace("hm","hello"));
//print(str);
/********************/
/*
*
* substr(index,length)
* it start from the index and count from it
* do not change the original string
* it return new array with the required index and length
* the upper bound no proble be larger than
* the original string
* if the length with -ve then it return empty string
*
*/
//var str="abcdef";
//print(str.substr(2,3));//from 2 to 4..[2]to[2+(length-1)]
//print(str.substr(2,10));//from 2 to the end
//print(str.substr(2,-1));//""
//print(str);
////substr do not change the original string
/***********/
/*
*
* substring(index,index)
* start index with me
* end index not with me
* make new string and
* donot change the origianl one
*/
//var str="abcdef";
//print(str.substring(2,4));
//print(str);
////substring no change the original string
////the end index not with me
/************************/
/*
* toLowerCase()
* do not change in the original string
* return new array in lower case
*/
//str="heLLo";
//print(str.toLowerCase());
//print(str);
//print(str.toUpperCase());
//print(str);
//the same with toUpperCase
/***************************/
/*
* you can call the function before
* the defination of the function
*/
//myfunction("before");
//function myfunction(a)
//{
// alert("Hello from function " + a);
//}
//myfunction("after");
/****************************/
//alert(add(1, 2, 3));//1+2=3
//// the function will ignore parameter 3
//alert(add());//the value of a and b will be undefined
////undefined+undefined=NaN . . . not a number
//function add(a, b)
//{
// return a + b;
//}
/****************************/
/*
* function define new scope of variables
* the only new scope in all javascript
* the variables in the function are local to the function
*/
//xx = "hay";
//function sms()
//{
// x = 5;//this variable is globle
// //globle because no var declaration before it
// var message = "welcome from sms";//local for the function only
// alert(xx + " " + message);
//}
//sms();
//alert(x);//x is global variable no problem
//print(message);//error no thing in this scope call message
/*****************************/
/*.............#...............*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment