0

I am working with javascript, i have static value(15000) and i want 15000*1.5/100+15000 with 90 times,i mean after getting result i want to again calculate that value(result/output,for example 225) 90 times,How can i do this ?

<script>
const cars ="15000";
for (let i = 0; i < 90; i++) {
   cars = cars*1.5/100+15000;
   cars --;
 }
 </script>

In c++ i did with following code but now i want to convert to javascript code

#include <iostream>

int main() {
    float a,b,c;
    std::cin>>b;
    for(int i=0;i<90;i++)
    {
        a=b*1.5/100;
        c=a+b;
        std::cout<<c<<"\n";
        b=c;
    }
    std::cout << "Hello world!"<<c;

    return 0;
}

My expected output is

15000
15225
15453.4
15685.2
15920.5
16159.3
16401.6
...
4
  • If you want to be able to change the value of cars - then you should not declare it as const. Commented Jul 21, 2022 at 12:12
  • Your C++ example uses three variables for this; so why are you trying to do this with only a single one now in your JS version? Commented Jul 21, 2022 at 12:14
  • @CBroe what is the solution ? can you update/write code so i can check Commented Jul 21, 2022 at 12:14
  • @CBroe c++ program is working fine you can check in c++ compiler, but i am new in javascript so i just tried not sure about output,Hope you understand, Commented Jul 21, 2022 at 12:15

2 Answers 2

1

You can do something like this:

let cars = 15000;
for (let i = 0; i < 90; i++) {
  console.log(parseFloat(cars.toFixed(1))); // set 1 decimal and print it
  cars = (cars * 1.5) / 100 + cars; // same function as c++
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply use an array and push the result into for each iteration of the loop, then use the [i] index to use the new value. Finally you just have to output the array and you already have all your data stored.

let carsArray = [15000];

for (let i = 0; i < 90; i++) {
   cars = carsArray[i]*1.5/100 + carsArray[i];
   carsArray.push(cars);
 }
 
 console.log(carsArray);
 
 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.