# JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

### Tell us what’s happening:

Sorry if this is a silly question, but I’ve been getting many of these kind of undefined errors so far while I’ve been doing this course.

Why does it tell me that z is not defined?

I return it from the function I created, in order to use it in the subsequent part of the code. So I can’t understand what I must be doing wrong.

Although it is a local variable within the function findingBeginning, it becomes a global variable after I have returned it, no?

``````function convertToRoman(num) {

// Note: Roman Numerals do not go above 3999

// Necessary numerals and array for creating the corresponding Roman Numeral
const romanNumerals = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
const arabicNumerals = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
let correspondingNumeral = [];

// finding the highest Roman numeral present in the number passed, and returning the appropriate index in arabicNumerals and romanNumerals

function findingBeginning(num) {
for (let i = 0; i < romanNumerals.length; i++) {
let char = num / arabicNumerals[i];
if (char >=1) {
let z = i;
console.log(z);
break;
}
}
return z;
}

// Calling the function

findingBeginning(num);

// Creating variables for the loop

let remainder = 0;
let char = 0;

// Acquiring the appropriate Roman Numerals

for (let i = z; i < romanNumerals.length; i++) {
// only for the first iteration
if (i == z) {
char = num / arabicNumerals[i];
remainder = num % arabicNumerals[i];
} else {    //  for all iterations afterwards
char = num % arabicNumerals[i - 1]    // making char the remainder from the last iteration
check = char / arabicNumerals[i];  //making sure the current Roman Numeral is appropriate, and if not, moving to the next one down
if (check >= 1) {
continue;
} else {
break;
}
}
for (let p = 0; p < (char + 1); p++) {
correspondingNumeral.push(romanNumerals[i]);
}
}

// Construct string from correspondingNumeral and return it

}

convertToRoman(36);
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36`

### Challenge Information:

JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

Hi @anthonyl,

In your example, `z` is only defined in this block:

``````if (char >=1) {
let z = i;
console.log(z);
break;
}
``````

That is because `let` and `const` are block-scoped variables that will only keep a variable alive during the block in which they are created. You can’t return `z` from that function because it isn’t available except for in that if-statement. But, if you were to take `z` and set it on the first line of your primary function `convertToRoman`, it will be available to the entire function and any sub-functions such as `findingBeginning`.

I haven’t evaluated anything else beyond that. This is definitely going to need some work before you’re done.

You may want to go back and rework some of the JavaScript lessons to better understand about the scope of variables, especially when they are created with the keywords `let` and `const` if you’re still having trouble.

2 Likes

Hi Marcus,

Oh… That’s the explanation I’ve been looking for over the past month or so. I had suspected there must be something like that, because I don’t recall any issue like that when declaring variables through conditional statements in Python, but I hadn’t been able to do it while I’ve been going through this course.

I’ll follow what you suggest and review variable scope.

1 Like

Just out of interest, I take it that the inability to use variables outside of a conditional statement that they are defined in is considered by programmers to be a fairly large limitation in JavaScript?

Or is it pretty common in other languages as well?

Variable scope is more of a necessity than a limitation. It actually gives you far more control over your variables and your code in general, and helps to avoid a lot of potential pitfalls and bugs.

1 Like

As @igorgetmeabrain said, you need scoping when creating variables, especially when you do more complex operations, for efficiency’s (and memory’s) sake. And it is very common for variables to have certain scopes where/how they are defined in all of the major programming languages.