//random number from 1 to Nvar random = Math.floor(Math.random() * N + 1); |
Thursday, July 3, 2014
Generate Random number from 1 to N in javascript
Javascript Function To Remove Array element by Value
function removeByValue(arr, val) { for(var i=0; i<arr.length; i++) { if(arr[i] == val) { arr.splice(i, 1); break; } }}var somearray = ["mon", "tue", "wed", "thur"]removeByValue(somearray, "tue");//somearray will now have "mon", "wed", "thur"Javascript Function To Remove Array element by Index
function removeByIndex(arr, index) { arr.splice(index, 1);}test = new Array();test[0] = 'Apple';test[1] = 'Ball';test[2] = 'Cat';test[3] = 'Dog';alert("Array before removing elements: "+test);removeByIndex(test, 2);alert("Array after removing elements: "+test);Javascript Function to calculate present value of an investment.
// Function to calculate present value of an investment..
// Parameters are rate, total number of periods, payment made each period, future value and type (when payments are due)
function pv(rate, per, nper, pmt, fv) {
nper = parseFloat(nper);
pmt = parseFloat(pmt);
fv = parseFloat(fv);
rate = eval((rate) / (per * 100));
if (nper == 0) {
//(pmt == 0) ||
alert("Why do you want to test me with zeros?");
return (0);
}
if (rate == 0) // Interest rate is 0
{
pv_value = -(fv + (pmt * nper));
}
else {
x = Math.pow(1 + rate, -nper);
y = Math.pow(1 + rate, nper);
pv_value = -(x * (fv * rate - pmt + y * pmt)) / rate;
}
pv_value = conv_number(pv_value, 2);
return (pv_value);
}
function conv_number(expr, decplaces) { // This function is from David Goodman's Javascript Bible.
var str = "" + Math.round(eval(expr) * Math.pow(10, decplaces));
while (str.length <= decplaces) {
str = "0" + str;
}
var decpoint = str.length - decplaces;
return (str.substring(0, decpoint) + "." + str.substring(decpoint, str.length));
}
//end of pv
// Parameters are rate, total number of periods, payment made each period, future value and type (when payments are due)
function pv(rate, per, nper, pmt, fv) {
nper = parseFloat(nper);
pmt = parseFloat(pmt);
fv = parseFloat(fv);
rate = eval((rate) / (per * 100));
if (nper == 0) {
//(pmt == 0) ||
alert("Why do you want to test me with zeros?");
return (0);
}
if (rate == 0) // Interest rate is 0
{
pv_value = -(fv + (pmt * nper));
}
else {
x = Math.pow(1 + rate, -nper);
y = Math.pow(1 + rate, nper);
pv_value = -(x * (fv * rate - pmt + y * pmt)) / rate;
}
pv_value = conv_number(pv_value, 2);
return (pv_value);
}
function conv_number(expr, decplaces) { // This function is from David Goodman's Javascript Bible.
var str = "" + Math.round(eval(expr) * Math.pow(10, decplaces));
while (str.length <= decplaces) {
str = "0" + str;
}
var decpoint = str.length - decplaces;
return (str.substring(0, decpoint) + "." + str.substring(decpoint, str.length));
}
//end of pv
Javascript Function to calculate future value of an investment..
// Function to calculate future value of an investment..
function fv(rate, per, nper, pmt, pv) {
nper = parseFloat(nper);
pmt = parseFloat(pmt);
pv = parseFloat(pv);
rate = eval((rate) / (per * 100));
if ((nper == 0)) {
//(pmt == 0) ||
alert("Why do you want to test me with zeros?");
return (0);
}
if (rate == 0) // Interest rate is 0
{
fv_value = -(pv + (pmt * nper));
}
else {
x = Math.pow(1 + rate, nper);
// y = Math.pow(1 + rate, nper);
fv_value = -(-pmt + x * pmt + rate * x * pv) / rate;
}
fv_value = conv_number(fv_value, 2);
return (fv_value);
}
//end of future value
function fv(rate, per, nper, pmt, pv) {
nper = parseFloat(nper);
pmt = parseFloat(pmt);
pv = parseFloat(pv);
rate = eval((rate) / (per * 100));
if ((nper == 0)) {
//(pmt == 0) ||
alert("Why do you want to test me with zeros?");
return (0);
}
if (rate == 0) // Interest rate is 0
{
fv_value = -(pv + (pmt * nper));
}
else {
x = Math.pow(1 + rate, nper);
// y = Math.pow(1 + rate, nper);
fv_value = -(-pmt + x * pmt + rate * x * pv) / rate;
}
fv_value = conv_number(fv_value, 2);
return (fv_value);
}
//end of future value
Javascript Function to calculate Periodic Payments(PMT)
// Function to calculate Periodic Payments.
function pmt(rate, per, nper, pv, fv) {
fv = parseFloat(fv);
nper = parseFloat(nper);
pv = parseFloat(pv);
per = parseFloat(per);
if ((per == 0) || (nper == 0)) {
alert("Why do you want to test me with zeros?");
return (0);
}
rate = eval((rate) / (per * 100));
if (rate == 0) // Interest rate is 0
{
pmt_value = -(fv + pv) / nper;
}
else {
x = Math.pow(1 + rate, nper);
pmt_value = -((rate * (fv + x * pv)) / (-1 + x));
}
pmt_value = conv_number(pmt_value, 2);
return (pmt_value);
}
//end Of PMT
function pmt(rate, per, nper, pv, fv) {
fv = parseFloat(fv);
nper = parseFloat(nper);
pv = parseFloat(pv);
per = parseFloat(per);
if ((per == 0) || (nper == 0)) {
alert("Why do you want to test me with zeros?");
return (0);
}
rate = eval((rate) / (per * 100));
if (rate == 0) // Interest rate is 0
{
pmt_value = -(fv + pv) / nper;
}
else {
x = Math.pow(1 + rate, nper);
pmt_value = -((rate * (fv + x * pv)) / (-1 + x));
}
pmt_value = conv_number(pmt_value, 2);
return (pmt_value);
}
//end Of PMT
Javascript Function To calculate Effective RATE
//Function To calculate Effective RATE
function RATE(annualrate, nper) {
var rate;
annualrate = 1 + (annualrate / 100);
rate = parseFloat(Math.pow(annualrate, 1 / nper) - 1)
return (rate);
}
function RATE(annualrate, nper) {
var rate;
annualrate = 1 + (annualrate / 100);
rate = parseFloat(Math.pow(annualrate, 1 / nper) - 1)
return (rate);
}
Subscribe to:
Posts (Atom)