From d33562ea344422a7028ff46650ebc06385d075e3 Mon Sep 17 00:00:00 2001 From: yikth Date: Sun, 30 Aug 2020 07:51:37 +0800 Subject: [PATCH] add javascripts basic --- JavaScriptBasics/if.js | 28 ++++++++++++++++++++++++++++ JavaScriptBasics/simpleloop.js | 21 +++++++++++++++++++++ JavaScriptBasics/stringMethods.js | 19 +++++++++++++++++++ JavaScriptBasics/variables.js | 24 ++++++++++++++++++++++++ playground/variables.js | 3 ++- 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 JavaScriptBasics/if.js create mode 100644 JavaScriptBasics/simpleloop.js create mode 100644 JavaScriptBasics/stringMethods.js create mode 100644 JavaScriptBasics/variables.js diff --git a/JavaScriptBasics/if.js b/JavaScriptBasics/if.js new file mode 100644 index 0000000..f0e893b --- /dev/null +++ b/JavaScriptBasics/if.js @@ -0,0 +1,28 @@ +var price = 1000; + +/* +if (price > 100) { //true or false ---- Booloen + console.log ("You have 10% dicount for this item "); + console.log ("log again"); +} +if (price === 100) { //note the operator! + console.log ("You have 8% discunt for this item"); +} +if (price === 50) { //note the operator! + console.log ("Purchase for 100 or more to get discount") +} +*/ + + + +if (price > 100) { + console.log ("You have 10% dicount for this item "); +} else if (price === 100) { + console.log ("You have 8% discunt for this item"); + } else if (price === 50) { + console.log ("Purchase for 100 or more to get discount"); + } else { + console.log("invalid data"); + } + + \ No newline at end of file diff --git a/JavaScriptBasics/simpleloop.js b/JavaScriptBasics/simpleloop.js new file mode 100644 index 0000000..6ebf928 --- /dev/null +++ b/JavaScriptBasics/simpleloop.js @@ -0,0 +1,21 @@ +/* +for (var i=0; i<10; i=i+2) { //i - loop control variable + console.log(i); +} +*/ + + +for (i=0; i<10; i++){ + for(j=0; j<5; j++){ + console.log(j); + } +} + + +/* + var i = 0; + while (i<10){ + console.log(i); + i++; +} +*/ \ No newline at end of file diff --git a/JavaScriptBasics/stringMethods.js b/JavaScriptBasics/stringMethods.js new file mode 100644 index 0000000..6d00d19 --- /dev/null +++ b/JavaScriptBasics/stringMethods.js @@ -0,0 +1,19 @@ +//Some essential string object methods +var myStr = "I'm feeling great."; +var l = myStr.length; +console.log("charater count:" + l); +console.log(myStr[2]); //m +console.log(myStr[4]); //f + +var subStr1 = myStr.slice(4,11); // returns starting position and up to (but not including) the ending position +var subStr2 = myStr.slice(12); //returns part of the string from position 10 +console.log(subStr1); +console.log(subStr2); +var p1 = myStr.indexOf('feel');//returns 4, ie the substring is at pos 10 +var p2 = myStr.indexOf('test');// returns -1, which means not found +console.log(p1 + ", " +p2) + +console.log(myStr.replace('feel','game')) +console.log(myStr.toLowerCase());//convert entire string to lowercase +console.log(myStr.toUpperCase());//convert entire string to uppercase + diff --git a/JavaScriptBasics/variables.js b/JavaScriptBasics/variables.js new file mode 100644 index 0000000..fcc2c5b --- /dev/null +++ b/JavaScriptBasics/variables.js @@ -0,0 +1,24 @@ +/* variables, numsbers, strings, mixed type operations, +template literals, Boolean, semicolon, LET/VAR, case-sensitive, comments */ +var a = 10; //a is a name for memory location +let b = 20.5; +let givenName = "James" +var lastName = 'LIM' +var myText = "I'm feeling great."; +a = 20; +var c; +console.log(c); //undefined! +var d = null; +//console.log(D); //JS is case sensitive +console.log(d); +c = a+b; +d = a + lastName +const e= 10; //constant +//e = a+b; +let f = e < b; +let g = f + 1; +console.log(`Hello ${givenName}`); // +console.log("Hello " + givenName); +console.log(`value of a is ${a}, value of b is ${b}, value of c is ${c}, value of d is ${d}, value of e is ${e}`); +console.log(`value of f is ${f}, value of g is ${g}`); +console.log("I'm " + givenName + " " + lastName + ". " + myText); diff --git a/playground/variables.js b/playground/variables.js index a06159a..f2212f0 100644 --- a/playground/variables.js +++ b/playground/variables.js @@ -9,7 +9,8 @@ var d = 55; // template literal `..` console.log(`My answer is ${d}`); -var myStr = 'Hello World'; +var myStr = "Hello \" World"; +console.log(myStr); var x = 10; var y = 20;