add sample Javascript Basic code
parent
1e29734cb2
commit
02744fb66f
@ -0,0 +1,21 @@
|
|||||||
|
var names = ['Tom','Peter','Mary'];
|
||||||
|
var name_string = names.join(',');
|
||||||
|
console.log(name_string);
|
||||||
|
|
||||||
|
myArray_string = "ABS Bank, 7000.00, true, Savings Account";
|
||||||
|
var accDetails = myArray_string.split(',');
|
||||||
|
console.log(accDetails);
|
||||||
|
console.log("My account balance is: $" + accDetails[1]);
|
||||||
|
|
||||||
|
//add/delete at the end
|
||||||
|
console.log("pop :"); names.pop(); console.log(names);
|
||||||
|
console.log("push :"); names.push('James'); console.log(names);
|
||||||
|
|
||||||
|
//add at the end and delete from the front
|
||||||
|
console.log("shift :"); names.shift(); console.log(names);
|
||||||
|
console.log("unshift :"); names.unshift('Lim'); console.log(names);
|
||||||
|
//add/delete at middle
|
||||||
|
console.log(names.splice(1, 1, ["Lim", "jenny"]));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
/* A short demo for arrays */
|
||||||
|
var myArray = ["ABS Bank", 7000.00, true, 'Savings Account'];
|
||||||
|
console.log("Your bank: " + myArray[0]);
|
||||||
|
console.log('You have mede a transaction today: ' + myArray[2]);
|
||||||
|
console.log('Array size:' + myArray.length);
|
||||||
|
//Arrays can contain arrays as members. (2D arrays - Table)
|
||||||
|
var grocery_list = [[5,'egg'],[10,'bread'],[12,'milk']];
|
||||||
|
|
||||||
|
|
||||||
|
console.log(grocery_list[2][1]); // returns 'milk', 2nd item of 3rd array
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
//let a = 10; defining a funtion has similar syntax
|
||||||
|
let depositIntRates = function() {
|
||||||
|
console.log("3% for first $10000");
|
||||||
|
console.log("3.5% for amount greater than 10000");
|
||||||
|
}
|
||||||
|
depositIntRates();
|
||||||
|
|
||||||
|
let futureValue = function(principle, rate, years, ci =2) { //ci - Number of Times interest is Compounded per period (or year)
|
||||||
|
rate = rate/100;
|
||||||
|
return (principle * (1 + rate/ci)**(ci * years));
|
||||||
|
}
|
||||||
|
var ans = futureValue(10000, 3, 5, 1).toFixed(2);
|
||||||
|
console.log(ans);
|
||||||
|
|
||||||
|
var a = 10.32324;
|
||||||
|
console.log(a/2);
|
||||||
|
console.log((a/2).toFixed(2));
|
||||||
|
//console.log(b);
|
||||||
|
|
||||||
|
let futureValueAF = (principle, rate, years, ci =2) => { //Arrow Function
|
||||||
|
rate = rate/100;
|
||||||
|
return (principle * (1 + rate/ci)**(ci * years));
|
||||||
|
}
|
||||||
|
console.log(futureValueAF(10000, 3, 5, 1).toFixed(2));
|
||||||
|
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
//function without argument
|
||||||
|
function depositIntRates() {
|
||||||
|
console.log("3% for first $10000");
|
||||||
|
console.log("3.5% for amount greater than 10000");
|
||||||
|
}
|
||||||
|
depositIntRates();
|
||||||
|
|
||||||
|
//function with arguments
|
||||||
|
function calcInterest(amount) { 5000
|
||||||
|
var interest = 0;
|
||||||
|
var balance = 0;
|
||||||
|
if (amount > 10000) {
|
||||||
|
balance = amount - 10000;
|
||||||
|
}
|
||||||
|
int = 10000 * .03 + balance * 0.035;
|
||||||
|
return (int);
|
||||||
|
}
|
||||||
|
|
||||||
|
var output = calcInterest(20000);
|
||||||
|
console.log(output);
|
||||||
Loading…
Reference in New Issue