Calculation of Investment Balance in C# or javascripts [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Formula
Investment Balance (Y) =  P*(1+r)^Y +  C *(((1+r)^(Y+1) –
(1+r))/r)
Where:
Y = number of years invested (investment time frame)
P = principal investment amount (current $ value of initial investment to be made)
r = assumed rate of return p.a. (expressed as a decimal, so 5% return would be 0.05)
C = amount of regular contributions pa
Salary: $100,000
Y: 20 years
P: $50,000
r: 0.05
C: 6521
output: 359069.1263
Hey guys, I have this formula I wanted to implement it in C# or JavaScript. I tried a lot but I didn't get the expected output so cloud anyone please describe or write a sample code that I can understand the implementation. It will be highly appreciated.

I don't know which problems did you have writing this in C#, but the code is very simple:
var y = 20;
var p = 50000;
var r = 0.05;
var c = 6521;
var result = p * Math.Pow(1 + r, y) + c * ((Math.Pow(1 + r, y + 1) - (1 + r))/r);
JavaScript:
var y = 20;
var p = 50000;
var r = 0.05;
var c = 6521;
var result = p * Math.pow(1 + r, y) + c * ((Math.pow(1 + r, y + 1) - (1 + r))/r);
You should only assign your values to variables.

Related

Debugging small javascript with exponents [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
n = 1;
while (n < 1024) {
alert(Math.pow(n, 2));
n = n + 2;
}
How can I make this display the powers of 2 from 2^0 to 2^10 inclusive i.e. (1-1024) and end once it reaches 1024 in Javascript
I would do something like this:
for(let n = 0; n <= 10; n++) {
console.log(2 ** n)
}
This logs the answer of each loop iteration. As Pointy suggested in the comment, we have to make the n value iterate from 1 to 10, since that is the variable that generates the output, whereas the 1024 is simply the output that comes from the power values. You could also use Math.pow as follows:
for(let n = 0; n <= 10; n++) {
console.log(Math.pow(2, n))
}
Implementing Secan's suggestion, here is a function which will take any number as a parameter:
function foo(x, exp) {
for(let i = 0; i <= exp; i++) {
console.log(x ** i)
}
}
So to apply this to the original answer, we would call it like this:
foo(2, 10)
Sorry for the ambuiguity with my original question. I've figured it out-
n=0;
while (n <= 10) {
alert(Math.pow(2, n));
n = n + 1;
}
Setting n=0, n<=10 in while and changing n + 1 instead of 2 resolved my issue. Thanks for your responses!

Javascript. The calc() function does not work as soon as i add the math.floor function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
the function works properly when instead of math.floor(math.random() * 10 + 1); i use math.random(). why does adding math.floor stop the program from working?
for(x=0; x< 5; x++){
var a = Math.floor(Math.random() * 10 +1) ;
var b = math.floor(Math.random() *10 +1) ;
var c = math.floor(Math.random() *10 +1) ;
var d = math.floor(Math.random() *10 +1) ;
document.write((a*b)/(c*d)+"<br>") ;
You are using lowercase math instead of TitleCase Math.
for(x=0; x< 5; x++){
var a = Math.floor(Math.random() * 10 +1) ;
var b = Math.floor(Math.random() *10 +1) ;
var c = Math.floor(Math.random() *10 +1) ;
var d = Math.floor(Math.random() *10 +1) ;
}
document.write((a*b)/(c*d)+"<br>") ;
Because the M in Math has to be uppercase, as in your first variable assignment:
var a = Math.floor(Math.random() * 10 + 1) ;

Get the percentage of a number with javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So I am creating a web app and I get some data.
There are points you have now (now=284), and total points (total=1000). So, The difference between them is dif=716.
How do I use javascript to turn the difference into a percentage value, for example, 32% or whatever?
Thanks
This is a math question, not programming, but you ask for javascript here you are an example:
var value = 249;
var total = 1000;
var calcPercent = function(v, t) {
return 100*v/t;
};
alert(calcPercent(value, total)+"%");
var total = 1000,
subtract = 284;
var differencePercentage = ((total - subtract) / total) * 100; // 71.6
Simple math would be
var now = 284;
var total = 1000;
console.log( "percentage is " + (now* 100/total) );
console.log( "Negative percentage is " + (100-(now* 100/total)) );
(function(){
var now = 284;
var total = 1000;
var difference = ((total - now) / total) * 100;
alert(difference);
})()

How was this .js archive encrypted? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
How was this code encrypted? A former webmaster left me code encrypted this way. I do not know how to solve.
Code:
function setCookie(a, b, c) {
var d = new Date();
d[_0x6fff[1]](d[_0x6fff[0]]() + 24 * c * 60 * 60 * 1e3);
var e = _0x6fff[2] + d[_0x6fff[3]]();
document[_0x6fff[4]] = a + _0x6fff[5] + b + _0x6fff[6] + e;
}
function getCookie(a) {
var b = a + _0x6fff[5];
var c = document[_0x6fff[4]][_0x6fff[8]](_0x6fff[7]);
for (var d = 0; d < c[_0x6fff[9]]; d++) {
var e = c[d];
while (_0x6fff[11] == e[_0x6fff[12]](0)) e = e[_0x6fff[10]](1);
if (e[_0x6fff[13]](b) != -1) return e[_0x6fff[10]](b[_0x6fff[9]], e[_0x6fff[9]]);
}
return _0x6fff[14];
}
Looks like it's a combination of minification and Hex-encoded Chinese characters:
Minification is a way of reducing the size of a javascript file by replacing long variable names with single letters (a, b, c in your example above)
_0x6fff is the HEX representation of a HAN character: cross on stepping-stones
Once code has been minified, you can't really undo it. See here

Draw path of divs between two divs [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to position n divs in between two absolute positioned divs? Im creating some sort of map and would like to draw footprints between two points. I was wondering if i could calculate coordinates of first and second div and determine how many footprints (with fixed width) would go in between and than somehow absolute position them with top and left position.
I don't know how to put this together in jQuery or Javascript.. Any ideas??
Thank you in advance!
Well, this is what I came up with:
Fiddle
There can be as many pairs of points as you wish, one has the class first, the other having the class second, and they will be paired with steps thanks to the following:
var f = $('.first');
var s = $('.second');
f.each(function(i) {
var f2 = f.eq(i);
var s2 = s.eq(i);
var xdist = s2.position().left - f2.position().left;
var ydist = s2.position().top - f2.position().top;
var dist = Math.sqrt(xdist*xdist + ydist*ydist); // Pythagoras
var numOfSteps = Math.round(dist / 50) - 1;
var stepX, stepY;
for (var l = 0; l < numOfSteps; l++) {
stepX = xdist/(numOfSteps+1)*(l+1) +
f2.width()/2 + f2.position().left;
stepY = ydist/(numOfSteps+1)*(l+1) +
f2.height()/2 + f2.position().top;
$('<div></div>').addClass('step').appendTo($(document.body))
.css('left', stepX + 'px')
.css('top' , stepY + 'px');
}
});
It's pretty much understandable without any comments, but if you can't figure out something, feel free to ask.
The number of footprints is calculated here:
var numOfSteps = Math.round(dist / 50) - 1;
and so to change the amount of footprints, change the number 50 to something else (the bigger the number, the less footprints).
Glad to help, was fun coming up with this ;)

Categories

Resources