I just wrote the code below where I form the Roman numeral array by first pushing the M’s, then the D’s etc and the last to be pushed is I’s.
However, after pushing the M’s and before adding anything else to the Roman numeral array, when I console.log the array and use an app other than Codepen (such as Brackets) together with the web console in either Firefox or Google, it returns the completed array for each and all of my console.log statements. This is not what I expected. Instead, I expected the console logs to return the partial array showing the array being formed.
In contrast, when I run the same code in Codepen with link:
the web console in Codepen shows the array being populated, which is what I would expect, but this behaves different from using a non-Codepen app such as Brackets together with a web console in Firefox or Chrome.
Could someone please explain 1) why does the web console in Firefox and Chrome behave this way?
and 2) Why does the web console in Firefox and Chrome behave differently than the web console in Codepen?
The Javascript code is:
function convertToRoman(num) {
var singles = num % 5;
var fives = Math.floor((num % 10) / 5);
var tens = Math.floor((num % 50) / 10);
var fifties = Math.floor((num % 100) / 50);
var hundreds = Math.floor((num % 500) / 100);
var fivehunds = Math.floor((num % 1000) / 500);
var thousands = Math.floor((num) / 1000);
console.log("the singles count is: ", singles);
console.log("the fives count is: ", fives);
console.log("the tens count is: ", tens);
console.log("the fifties count is: ", fifties);
console.log("the hundreds count is: ", hundreds);
console.log("the fivehunds count is: ", fivehunds);
console.log("the thousands count is: ", thousands);
var romarr = [];
for (var m = 1; m <= thousands; m++) {
romarr.push("M");
}
console.log("1000's array is: ", romarr);
// Why is the completed array displayed instead of the array with M's only????
if (fivehunds == 1) {
romarr.push("D");
}
console.log("500's array is: ", romarr);
for (var k = 1; k <= hundreds; k++) {
romarr.push("C");
}
console.log("100's array is: ", romarr);
if (fifties == 1) {
romarr.push("L");
}
console.log("50's array is: ", romarr);
for (var j = 1; j <= tens; j++) {
romarr.push("X");
}
console.log("10's array is: ", romarr);
if (fives == 1) {
romarr.push("V");
}
console.log("5's array is: ", romarr);
for (var i = 1; i <= singles; i++) {
romarr.push("I");
}
console.log("final array is: ", romarr);
num = romarr.join("");
console.log("the joined array is: ", num);
return num;
}
convertToRoman(1023);