# Project Euler Problems 1 to 100 - Problem 17: Number letter counts

### Tell us what’s happening:

I have tried the extremes of the function in order to know if that was the problem. I got the correct answer in relation the third test, but I do not know what to do in relation to the last test.

### Your code so far

``````function numberLetterCounts(limit) {
let listOfNumberLengths = {
0: 0,
1:"one".length,
2:"two".length,
3:"three".length,
4:"four".length,
5:"five".length,
6:"six".length,
7: "seven".length,
8: "eight".length,
9: "nine".length,
10: "ten".length,
11: "eleven".length,
12: "twelve".length,
13: "thirteen".length,
14: "fourteen".length,
15: "fifteen".length,
16: "sixteen".length,
17: "seventeen".length,
18:"eighteen".length,
19: "nineteen".length,
20: "twenty".length,
30: "thirty".length,
40: "forty".length,
50: "fifty".length,
60: "sixty".length,
70: "seventy".length,
80: "eighty".length,
90: "ninety".length,
100: "onehundred".length,
200: "twohundred".length,
300: "threehundred".length,
400: "fourhundred".length,
500: "fivehundred".length,
600: "sixhundred".length,
700: "sevenhundred".length,
800: "eighthundred".length,
900: "ninehundred".length,
1000: "onethousand".length
};
let test =  "threehundred".length; //and
let number = 0;
let iteration = limit;
let summatory = 0;
let array = [10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000];

while(iteration>0){
number = iteration;
while(number>16){
for(let i = array.length;i>=0;i--){
if(number>array[i] && number>=98){
summatory = summatory + listOfNumberLengths[array[i]] + 3;
number = number - array[i];

}else if (number>=array[i]){
summatory = summatory + listOfNumberLengths[array[i]];
number = number - array[i];

};
};
};

summatory = summatory + listOfNumberLengths[number];
number = number - number;

iteration = iteration - 1;
};

return summatory;
}

console.log(numberLetterCounts(1000));
``````

### Your browser information:

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

### Challenge Information:

Project Euler Problems 1 to 100 - Problem 17: Number letter counts

1 Like

I got the solution, but I do not still like it. Do you have a suggestion?

``````function numberLetterCounts(limit){
let arrayOfReferences = [10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000];
let number=limit;
let summatoryOfNumbers=0;
let iteration =0;
let objectOfStrings={
0: 0,
1:"one".length,
2:"two".length,
3:"three".length,
4:"four".length,
5: "five".length,
6:"six".length,
7:"seven".length,
8:"eight".length,
9:"nine".length,
10:"ten".length,
11: "eleven".length,
12: "twelve".length,
13: "thirteen".length,
14:"fourteen".length,
15:"fifteen".length,
16:"sixteen".length,
17:"seventeen".length,
18:"eighteen".length,
19:"nineteen".length,
20:"twenty".length,
30:"thirty".length,
40:"forty".length,
50:"fifty".length,
60:"sixty".length,
70: "seventy".length,
80:"eighty".length,
90:"ninety".length,
100:"onehundred".length,
200:"twohundred".length,
300:"threehundred".length,
400:"fourhundred".length,
500: "fivehundred".length,
600: "sixhundred".length,
700: "sevenhundred".length,
800: "eighthundred".length,
900: "ninehundred".length,
1000: "onethousand".length
};
while(number>0){
iteration = number;
if(iteration>100 && iteration<1000 ){

for(let i = arrayOfReferences.length-1;i>=0;i--){
if(iteration>=arrayOfReferences[i]){
iteration = iteration -  arrayOfReferences[i];
summatoryOfNumbers = summatoryOfNumbers + objectOfStrings[arrayOfReferences[i]];

};
};

summatoryOfNumbers = summatoryOfNumbers + objectOfStrings[iteration] + 3;
iteration = iteration - iteration;
number = number - 1;

}else if(iteration<=100){

for(let i = arrayOfReferences.length-1;i>=0;i--){
if(iteration>=arrayOfReferences[i]){
iteration = iteration -  arrayOfReferences[i];
summatoryOfNumbers = summatoryOfNumbers + objectOfStrings[arrayOfReferences[i]];

};
};
summatoryOfNumbers = summatoryOfNumbers + objectOfStrings[iteration];
iteration = iteration - iteration;
number = number - 1;
}else{
for(let i = arrayOfReferences.length-1;i>=0;i--){
if(iteration>=arrayOfReferences[i]){
iteration = iteration -  arrayOfReferences[i];
summatoryOfNumbers = summatoryOfNumbers + objectOfStrings[arrayOfReferences[i]];

};
};
summatoryOfNumbers = summatoryOfNumbers + objectOfStrings[iteration];
iteration = iteration - iteration;
number = number - 1;
summatoryOfNumbers = summatoryOfNumbers - 24;

};

};

return summatoryOfNumbers
};
console.log(numberLetterCounts(1000))

``````

What don’t you like about it

1 Like

It sums 3 additionally every time a number that finishes in 00 because of the condition if(iteration>100 && iteration<1000 ). You need to take that from the result when you are making the tests. That is the reason why it works, but if you use the number 13000, you need to take 138 from the final result. It is less intuitive.