Calculator comparing your inflation rate to the Consumer Price Index

The name of the pictureThe name of the pictureThe name of the pictureClash 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>');







share|improve this question

















  • 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
















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>');







share|improve this question

















  • 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












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>');







share|improve this question













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>');









share|improve this question












share|improve this question




share|improve this question








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












  • 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















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?