Skip to content

Instantly share code, notes, and snippets.

@ketanghumatkar
Last active June 27, 2019 12:27
Show Gist options
  • Save ketanghumatkar/6c8f7e88d86cbb741947b4a8eca85e44 to your computer and use it in GitHub Desktop.
Save ketanghumatkar/6c8f7e88d86cbb741947b4a8eca85e44 to your computer and use it in GitHub Desktop.
Javascript basics examples for Intern training
var example = {
a: 'a',
b: 1,
c: function () {
return 'I am here!';
},
'good-example': 'Nice work'
}
example.a // 'a'
example.b // 1
example.c // function () { return 'I am here!' }
example['good-example'] // 'Nice work'
var example = ['a', 1, true, function() { return 4; }];
example[0] // 'a'
example[1] // 1
example[2] // true
example[3] // function() { return 4; }
console.log(1234);
// Assignment
function example (a, b) {
this.a = a;
this.b = b;
var p = 1;
this.getA = function () {
return this.a;
}
this.getB = function() {
return this.b;
}
}
var e = new example(1, 2);
e.a // 1
e.b // 2
e.getA() // 1
e.getB() // 2
e // example { a: 1, b: 2, getA: [Function], getB: [Function] }
var e = new example('a', 'b');
e.a // 1
e.b // 2
e.getA() // 1
e.getB() // 2
// expressive
var example = function () {
return 'example';
}
// declarative
function example () {
return 'example';
}
//
//
//original version
// Webhoisting
bar();
foo();
function foo () {
return a + b;
}
var bar = function () {
return 'example';
}
var abc = 10;
bar();
foo();
// parsed version
var bar;
var abc;
function foo () {
return 'ab';
}
// Webhoisting
bar(); // error
foo(); // 'ab'
bar = function () {
return 'example';
}
abc = 10;
bar(); // example
foo(); // 'ab'
//
//
// Write calculator program in Javascript using oops
// Program
function Calculator () {
this.add = function () {
}
this.minus = function () {
}
}
// Output
// Example 1
calculator = new Calculator()
calculator.evaluate('10 * 2')
//#=> 20
// Example 2
calculator.evaluate('kiwi + 5', {kiwi: 2})
//#=> 7
// Example 3 case insensitivity
calculator.evaluate('Kiwi + 5', {Kiwi: -2, kiwi: 2})
//#=> 7
calculator = new Calculator({caseSensitive: true})
calculator.evaluate('Kiwi + 5', { Kiwi: -2, kiwi: 2})
//#=> 3
// Example 4 storage
calculator.store({peaches: 15})
calculator.evaluate('peaches - 5')
// #=> 10
calculator.evaluate('peaches >= 15')
// #=> true
// Example 5 precedence
calculator.evaluate('5 + 3 * 2')
// #=> 11
calculator.evaluate('(5 + 3) * 2')
// #=> 16
// Example 6 rount function
calculator.evaluate('maximun(8, 2)')
// #=> 8
calculator.evaluate('round(8.2759, 2)')
// #=> 8.28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment