Thursday, March 23, 2023

Coding Challenge #30

// Are we in the upward slope or downward slope of the business cycle?
// Using m as a chart, get the trend of m over time(indices) to determine whether overall there is currently economic expansion or contraction.
var m = []; // monthly economic output
// m's values will be between 1 and 10 (inclusive)
for(var xint = 0;xint < 10;xint++) m.push(Math.floor(Math.random() * 9) + 1);

// get the overall trend
// the change is the accumulative difference between each index and the first index
var increases = 0;
var decreases = 0;
var change = m[0];
for(var xint = 1;xint < 10;xint++){
    if(m[xint] > m[xint-1]) increases++;
    else if(m[xint] < m[xint-1])decreases++;
    change += m[xint] - m[0];
}

console.log("m: " + m.toString());
console.log("Months of increased output: " + increases + " and " + decreases + " decreases.");
console.log("The overall change was " + change);
if(increases > decreases) console.log("More months where an increase occurred.");
else if(increases < decreases) console.log("More months where a decrease occurred.");
else console.log("Equal months of increases/decreases.");
if(change > m[0]) console.log("Overall, economic output is on the rise; expansion.");
else if(change < m[0]) console.log("Overall, economic output is on the decline; contraction.");
else console.log("Overall, economic output is consistent; economic output relatively unchanged.");

No comments:

Post a Comment

Coding Challenge #54 C++ int to std::string (no stringstream or to_string())

Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...