Calculator comparing your inflation rate to the Consumer Price Index
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
This code works fine and outputs the expected result. It is a small part of a larger program I'm working on to calculate personal inflation. I feel there is a better or more elegant way do this. I am trying to improve my programming logic skills so I hope its ok to ask for this kind of help.
I'm taking two arrays, both with 12 decimal numbers, dividing them by each other which will return an answer that is either above or below 1. I'm updating the above and below variables accordingly as counters. Then the conditional statements at the bottom are deciding which variable (above, below, avg) to be set to true. Based on which is true an arrow will be displayed as up, down or straight.
let personalCPI = [99.8, 100.8, 101.9,
102.1, 102.1, 101.9,
101.7, 102.5, 102.5,
102.6, 102.1, 102.2];
const nationalCPI = [99.5, 100.4, 101.4,
101.4, 101.4, 101.2,
101.1, 101.7, 101.6,
101.5, 101.1, 101.2];
let index;
let avg;
let above = 0;
let below = 0;
for (let i = 0; i < personalCPI.length; i++)
index = personalCPI[i] / nationalCPI[i];
if (index > 1)
above += 1;
else if (index < 1)
below += 1;
if (above > below)
above = true;
below = false;
else if (above >= 5 && above < 7 || below >= 5 && below < 7)
avg = true;
above = false;
below = false;
else
below = true;
above = false;
console.log(above);
console.log(below);
console.log(avg);
if (avg)
$("#inflation").attr("title", "Inflation is affecting you close to the
National Average");
$("#inflation").html('<i class="fas fa-arrows-alt-h fa-2x"></i>');
else if (below)
$("#inflation").attr("title", "Inflation is affecting you below the
National Average");
$("#inflation").html('<i class="fas fa-arrow-down fa-2x"></i>');
else if (above)
$("#inflation").attr("title", "Inflation is affecting you above the
National Average");
$("#inflation").html('<i class="fas fa-arrow-up fa-2x"></i>');
javascript jquery array calculator
add a comment |Â
up vote
1
down vote
favorite
This code works fine and outputs the expected result. It is a small part of a larger program I'm working on to calculate personal inflation. I feel there is a better or more elegant way do this. I am trying to improve my programming logic skills so I hope its ok to ask for this kind of help.
I'm taking two arrays, both with 12 decimal numbers, dividing them by each other which will return an answer that is either above or below 1. I'm updating the above and below variables accordingly as counters. Then the conditional statements at the bottom are deciding which variable (above, below, avg) to be set to true. Based on which is true an arrow will be displayed as up, down or straight.
let personalCPI = [99.8, 100.8, 101.9,
102.1, 102.1, 101.9,
101.7, 102.5, 102.5,
102.6, 102.1, 102.2];
const nationalCPI = [99.5, 100.4, 101.4,
101.4, 101.4, 101.2,
101.1, 101.7, 101.6,
101.5, 101.1, 101.2];
let index;
let avg;
let above = 0;
let below = 0;
for (let i = 0; i < personalCPI.length; i++)
index = personalCPI[i] / nationalCPI[i];
if (index > 1)
above += 1;
else if (index < 1)
below += 1;
if (above > below)
above = true;
below = false;
else if (above >= 5 && above < 7 || below >= 5 && below < 7)
avg = true;
above = false;
below = false;
else
below = true;
above = false;
console.log(above);
console.log(below);
console.log(avg);
if (avg)
$("#inflation").attr("title", "Inflation is affecting you close to the
National Average");
$("#inflation").html('<i class="fas fa-arrows-alt-h fa-2x"></i>');
else if (below)
$("#inflation").attr("title", "Inflation is affecting you below the
National Average");
$("#inflation").html('<i class="fas fa-arrow-down fa-2x"></i>');
else if (above)
$("#inflation").attr("title", "Inflation is affecting you above the
National Average");
$("#inflation").html('<i class="fas fa-arrow-up fa-2x"></i>');
javascript jquery array calculator
1
Can you add a pure English description about what it is that this code does?
â Simon Forsbergâ¦
Apr 4 at 13:47
I added a small bit of extra code and better description, I hope its easier to understand now.
â Darren
Apr 4 at 13:58
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This code works fine and outputs the expected result. It is a small part of a larger program I'm working on to calculate personal inflation. I feel there is a better or more elegant way do this. I am trying to improve my programming logic skills so I hope its ok to ask for this kind of help.
I'm taking two arrays, both with 12 decimal numbers, dividing them by each other which will return an answer that is either above or below 1. I'm updating the above and below variables accordingly as counters. Then the conditional statements at the bottom are deciding which variable (above, below, avg) to be set to true. Based on which is true an arrow will be displayed as up, down or straight.
let personalCPI = [99.8, 100.8, 101.9,
102.1, 102.1, 101.9,
101.7, 102.5, 102.5,
102.6, 102.1, 102.2];
const nationalCPI = [99.5, 100.4, 101.4,
101.4, 101.4, 101.2,
101.1, 101.7, 101.6,
101.5, 101.1, 101.2];
let index;
let avg;
let above = 0;
let below = 0;
for (let i = 0; i < personalCPI.length; i++)
index = personalCPI[i] / nationalCPI[i];
if (index > 1)
above += 1;
else if (index < 1)
below += 1;
if (above > below)
above = true;
below = false;
else if (above >= 5 && above < 7 || below >= 5 && below < 7)
avg = true;
above = false;
below = false;
else
below = true;
above = false;
console.log(above);
console.log(below);
console.log(avg);
if (avg)
$("#inflation").attr("title", "Inflation is affecting you close to the
National Average");
$("#inflation").html('<i class="fas fa-arrows-alt-h fa-2x"></i>');
else if (below)
$("#inflation").attr("title", "Inflation is affecting you below the
National Average");
$("#inflation").html('<i class="fas fa-arrow-down fa-2x"></i>');
else if (above)
$("#inflation").attr("title", "Inflation is affecting you above the
National Average");
$("#inflation").html('<i class="fas fa-arrow-up fa-2x"></i>');
javascript jquery array calculator
This code works fine and outputs the expected result. It is a small part of a larger program I'm working on to calculate personal inflation. I feel there is a better or more elegant way do this. I am trying to improve my programming logic skills so I hope its ok to ask for this kind of help.
I'm taking two arrays, both with 12 decimal numbers, dividing them by each other which will return an answer that is either above or below 1. I'm updating the above and below variables accordingly as counters. Then the conditional statements at the bottom are deciding which variable (above, below, avg) to be set to true. Based on which is true an arrow will be displayed as up, down or straight.
let personalCPI = [99.8, 100.8, 101.9,
102.1, 102.1, 101.9,
101.7, 102.5, 102.5,
102.6, 102.1, 102.2];
const nationalCPI = [99.5, 100.4, 101.4,
101.4, 101.4, 101.2,
101.1, 101.7, 101.6,
101.5, 101.1, 101.2];
let index;
let avg;
let above = 0;
let below = 0;
for (let i = 0; i < personalCPI.length; i++)
index = personalCPI[i] / nationalCPI[i];
if (index > 1)
above += 1;
else if (index < 1)
below += 1;
if (above > below)
above = true;
below = false;
else if (above >= 5 && above < 7 || below >= 5 && below < 7)
avg = true;
above = false;
below = false;
else
below = true;
above = false;
console.log(above);
console.log(below);
console.log(avg);
if (avg)
$("#inflation").attr("title", "Inflation is affecting you close to the
National Average");
$("#inflation").html('<i class="fas fa-arrows-alt-h fa-2x"></i>');
else if (below)
$("#inflation").attr("title", "Inflation is affecting you below the
National Average");
$("#inflation").html('<i class="fas fa-arrow-down fa-2x"></i>');
else if (above)
$("#inflation").attr("title", "Inflation is affecting you above the
National Average");
$("#inflation").html('<i class="fas fa-arrow-up fa-2x"></i>');
javascript jquery array calculator
edited Apr 4 at 16:44
200_success
123k14142399
123k14142399
asked Apr 4 at 13:29
Darren
112
112
1
Can you add a pure English description about what it is that this code does?
â Simon Forsbergâ¦
Apr 4 at 13:47
I added a small bit of extra code and better description, I hope its easier to understand now.
â Darren
Apr 4 at 13:58
add a comment |Â
1
Can you add a pure English description about what it is that this code does?
â Simon Forsbergâ¦
Apr 4 at 13:47
I added a small bit of extra code and better description, I hope its easier to understand now.
â Darren
Apr 4 at 13:58
1
1
Can you add a pure English description about what it is that this code does?
â Simon Forsbergâ¦
Apr 4 at 13:47
Can you add a pure English description about what it is that this code does?
â Simon Forsbergâ¦
Apr 4 at 13:47
I added a small bit of extra code and better description, I hope its easier to understand now.
â Darren
Apr 4 at 13:58
I added a small bit of extra code and better description, I hope its easier to understand now.
â Darren
Apr 4 at 13:58
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191249%2fcalculator-comparing-your-inflation-rate-to-the-consumer-price-index%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Can you add a pure English description about what it is that this code does?
â Simon Forsbergâ¦
Apr 4 at 13:47
I added a small bit of extra code and better description, I hope its easier to understand now.
â Darren
Apr 4 at 13:58