Javascript Basic
Global var and Local var
That’s important !! need to understand diffrent of three examples.
// example 1
let TestValue = "Global";
function ChangeValue(){
let TestVaule = "Local";
}
document.write(TestValue);
// the answer is : Global
// example 2
let TestValue = "Global";
function ChangeValue(){
let TestVaule = "Local";
document.write(TestValue);
}
// the answer is : Local
// example 3
let TestValue = "Global";
function ChangeValue(){
TestVaule = "Local";
}
document.write(TestValue);
// the answer is : Local
Function as a value
Function is object in javascript. that means function can be value in javascript.
function a(){} // it means var a = function(){}
a = {
b:function(){ // b is methods of object a
}
}
// object a has key b and value function
function cal(func num){
return func(num)
}
function increase(num){
return num+1
}
function decrease(num){
return num-1
}
alert(cal(increase,1));
alert(cal(decrease,1));
Callback
function sortNumber(a,b){
return b-a;
}
var numbers = [20,10,9,8,7,6,5,4,3,2,1];
alert(numbers.sort(sortNumber)); // array [20,10,9,8,7,6,5,4,3,2,1]
// sortNumber is callback function.
Continue reading
Javascript Basic
Closure
A closure is the combination of a function and the lexical environment within which that function was declared.
//example
function outter(){
var title = 'coding everybody';
function inner(){
alert(title);
}
inner();
}
// result : coding everybody
// as you can see, inner function can access to the var title
// even though title is located outside of the function. That's a Closure
Private variable
function factory_movie(title){
return {
get_title : function (){
return title;
}
set_title : function(_title){
if(typeof _title === 'string'){
title = _title;
}else{
alert('subject should be string type');
}
}
}
}
ghost = factory_movie('Ghost in the shell');
matrix = factory_movie('Matrix');
alert(ghost.get_title); // Ghost in the shell
alert(matrix.get_title); // Matrix
Parameter and Arguments
function sum(){
var i, _sum = 0;
for(i=0; i<arguments.length; i++){
// even though there is no parameter but js can access to arguments
document.write(i+' : '+arguments[i]+'<br />');
_sum += arguments[i];
}
return _sum;
}
document.write('result : ' + sum(1,2,3,4));
Apply
The apply() method calls a function with a given this value, and arguments provided as an array (or an array-like object).
o1 = {val:1, val2:2, val3:3}
o2 = {v1:10, v2:50, v3:100, v4:25}
function sum(){
var _sum = 0;
for(name in this){
_sum += this[name];
}
return _sum;
}
alert(sum.apply(o1)) // 6
alert(sum.apply(o2)) // 185
Continue reading
Day 0: Hello, World! Problem : Hello, World
My code :
function greeting(parameterVariable) {
// This line prints 'Hello, World!' to the console:
console.log('Hello, World!');
// Write a line of code that prints parameterVariable to stdout using console.log:
console.log(parameterVariable);
}
Day 0: Data Types Problem : Data Types
My code :
function performOperation(secondInteger, secondDecimal, secondString) {
// Declare a variable named 'firstInteger' and initialize with integer value 4.
const firstInteger = 4;
// Declare a variable named 'firstDecimal' and initialize with floating-point value 4.0.
const firstDecimal = 4.0;
// Declare a variable named 'firstString' and initialize with the string "HackerRank".
const firstString = 'HackerRank ';
console.log(parseInt(firstInteger) + parseInt(secondInteger));
// Write code that uses console.log to print the sum of the 'firstInteger' and 'secondInteger' (converted to a Number type) on a new line.
console.log(parseFloat(firstDecimal) + parseFloat(secondDecimal));
// Write code that uses console.log to print the sum of 'firstDecimal' and 'secondDecimal' (converted to a Number type) on a new line.
console.log(firstString + secondString);
// Write code that uses console.log to print the concatenation of 'firstString' and 'secondString' on a new line. The variable 'firstString' must be printed first.
Day 1: Arithmetic Operators Problem : Arithmetic Operators
My code :
function getArea(length, width) {
let area;
area = length * width;
// Write your code here
return area;
}
/**
* Calculate the perimeter of a rectangle.
*
* length: The length of the rectangle.
* width: The width of the rectangle.
*
* Return a number denoting the perimeter of a rectangle.
**/
function getPerimeter(length, width) {
let perimeter;
perimeter = 2 * (length + width);
// Write your code here
return perimeter;
}
Day 1: Functions Problem : Functions
My code :
/*
* Create the function factorial here
*/
function factorial(n) {
var result = 1;
for (var i=1; i<(n+1); i++) {
result *= i;
}
return result;
}
Day 1: Let and Const Problem : Let and Const
My code :
function main() {
const PI = Math.PI;
// Write your code here. Read input using 'readLine()' and print output using 'console.log()'.
const r = readLine();
console.log(PI * r * r);
// Print the area of the circle:
console.log(2 * r * PI);
// Print the perimeter of the circle:
Day 2: Conditional Statements: If-Else Problem : Conditional Statements: If-Else
My code :
Day 2: Conditional Statements: Switch Problem : Conditional Statements: Switch
My code :
Day 2: LoopsProblem : Loops
My code :
Day 3: Arrays Problem : Arrays
My code :
Day 3: Try, Catch, and Finally Problem : Try, Catch, and Finally
My code :
Day 3: Throw Problem : Throw
My code :
Day 4: Create a Rectangle Object Problem : Create a Rectangle Object
My code :
Day 4: Count Objects Problem : Count Objects
My code :
Day 4: Classes Problem : Classes
My code :
Day 5: Inheritance Problem : Inheritance
My code :
Day 5: Template Literals Problem : Template Literals
My code :
Day 5: Arrow Functions Problem : Arrow Functions
My code :
Day 6: Bitwise Operators Problem : Bitwise Operators
My code :
Day 6: JavaScript Dates Problem : JavaScript Dates
My code :
Day 7: Regular Expressions I Problem : Regular Expressions I
My code :
Day 7: Regular Expressions II Problem : Regular Expressions II
My code :
Day 7: Regular Expressions III Problem : Regular Expressions III
My code :
Day 8: Create a Button Problem : Create a Button
My code :
Day 8: Buttons Container Problem : Buttons Container
My code :
Day 9: Binary Calculator Problem : Binary Calculator
My code :
Continue reading