Is solution 2 to Roman Numeral Converter broken?

Is solution 2 to Roman Numeral Converter broken?
0

I mean this ->

units function doesn’t seem to be built to accept parameters and i variable is not defined when referenced in units function. Am I right?
It seems to work in Chrome Dev Tools but in challenge editor it returns this:

``````function convertToRoman(num) {
var romans = ["I", "V", "X", "L", "C", "D", "M"],
ints = [],
romanNumber = [],
numeral = "";
while (num) {
ints.push(num % 10);
num = Math.floor(num / 10);
}
console.log(num%10)
console.log(ints)
for (i = 0; i < ints.length; i++) {
units(ints[i]);
}
function units() {
numeral = romans[i * 2];
switch (ints[i]) {
case 1:
romanNumber.push(numeral);
break;
case 2:
romanNumber.push(numeral.concat(numeral));
break;
case 3:
romanNumber.push(numeral.concat(numeral).concat(numeral));
break;
case 4:
romanNumber.push(numeral.concat(romans[i * 2 + 1]));
break;
case 5:
romanNumber.push(romans[i * 2 + 1]);
break;
case 6:
romanNumber.push(romans[i * 2 + 1].concat(numeral));
break;
case 7:
romanNumber.push(romans[i * 2 + 1].concat(numeral).concat(numeral));
break;
case 8:
romanNumber.push(
romans[i * 2 + 1]
.concat(numeral)
.concat(numeral)
.concat(numeral)
);
break;
case 9:
romanNumber.push(romans[i * 2].concat(romans[i * 2 + 2]));
}
}
return romanNumber
.reverse()
.join("")
.toString();
}

// test here
convertToRoman(3000);

``````

return
0
[ 0, 0, 0, 3 ]
ReferenceError: i is not defined

How this is working in Chrome ?

Anyone ? Any moderator ? I think there should be an explanation for such a behaviour but I wouldnt leave that code among solutions.

You are getting `ReferenceError: i is not defined` error because in your code you never defined what is `i` i.e.

``````for (i = 0; i < ints.length; i++) {
``````

so define `i` as a variable using `var` i.e.

``````for (var i = 0; i < ints.length; i++) {
``````

1 Like

Thank you, for pointing this out.

I have edited the solution. Fatma is correct, the issue is that the variable `i` needs to be declared.

I believe this became an issue, because the challenge uses JavaScript’s

``````'use strict'
``````
1 Like

You mean the test suite actually use ‘use strict’ option which oblidge to declare variables ?

yes, all the challenges are in strict mode

2 Likes