PDA

View Full Version : Percentage and this


michshelly
02-01-2006, 02:33 PM
Hello,

Could someone please show me what is wrong with this code especially displaying a percentage.

function Investment() {
var startAmount = 10000; // $10 000
var AnnualInterest = startAmount % 15; // 15%
var totalSoFar = AnnualInterest * startAmount;
document.write("<p> Your total thus far ... $ " + totalSoFar + "</p>");
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>

Here's the problem I am trying to solve:

Write a script that calculates a !5% return on an investment of $10 000. Calculate the number of years required for a single $10 000 investment to reach $1 000 000 at an average annual return of 15%. Use a looping statement and assume that each iteration is equivalent to one year.

I haven't worked out the number of years to 1 million yet. But If you want to help with that, that would be great.

Thanks
Michelle

Kor
02-01-2006, 02:39 PM
despite the common look, the % is not the percent operator, it is the modulus (division remainder) operator. Javascript has no special percent operator. Just multiply and divide by 100

var a = 10000;
//15% from a is
var fifteenpercet = 15*a/100

michshelly
02-01-2006, 11:13 PM
Hi Kor,

Thanks very much for that. It makes sense!!

Michelle