Search code examples
javascriptnode.jsnested-loops

Can you use dynamic values in the instructions of a nested loop? and feedback on declaring variables (node.js)


Question Explanation (Zamka):
<----------------------------------------------------------------------------------------------------------> Input Example:

100  
500  
12  

1st Line: left bound (L)
2nd Line: right bound (D)
3rd Line: sum of digits within N/M (X)
<---------------------------------------------------------------------------------------------------------->
Output Example:

129 //focusing on this first
480  

1st Line: minimal integer that = X (N)
2nd Line: maximal integer that = X (M)
<---------------------------------------------------------------------------------------------------------->
Thought process:
1. declare input values with an array:

input = ['100','500','12'];

2. create loop between left (100) and right (500) bound:

for (let i = input[0]; i <= input[1]; i++)

3. declare a sum variable = 0 within the loop:

let sumMIN = 0;

4. create nested loop between 0 (first digit within i) and the length of i (i.length):

for (let j = 0; j < i.length; j++) //problem => stops at i = 100

5. for every digit within i sum up to sumMIN:

sumMIN += parseInt(i[j]);

6. if the sum of i's digits = X then return true:

if (sumMIN == input[2]) {
    console.log("true");
}

<---------------------------------------------------------------------------------------------------------->
Problem:
1. the nested loop only iterates i = 100
2. I think there is a problem lying within declaration of variables (string vs. integer)

My Code so far:

var input = ['100', '500', '12'];

function decipher() {

    for (let i = input[0]; i <= input[1]; i++) { //loops executes correctly
        let sumMIN = 0;
        for (let j = 0; j < i.length; j++) { //loop stops at i = 100
            sumMIN += parseInt(i[j]);
            console.log(sumMIN);
            if (sumMIN == input[2]) {
                console.log("true");
            } else {
                console.log("false");
            }
        }
    }
}

return decipher(input);

Current Output:

1
false
1
false
1
false

(let me know if you need more information!)


Solution

  • Based on the feedback of peers and comments, I was able to find a solution:

    var input = ['100', '500', '12'];
    
    function decipher() {
    
    for (let i = parseInt(input[0]); i <= parseInt(input[1]); i++) { //converted values to an integer
        var sumMIN = 0; //sets sum to 0 for every i
        var length1 = i.toString().length; //converts i to a string and finds the length
        var iString1 = i.toString(); //only converts i to a string
        for (let j = 0; j < parseInt(length1); j++) {  //converts length back to an integer
            sumMIN += parseInt(iString1[j]); //converts value to an integer to be able to do addition
        }
        if (sumMIN == parseInt(input[2])) { //converts value of 12 to an integer
            console.log(iString1);
            break; //stops when first "if" solution is found
        }
    }
    
    for (let i = parseInt(input[1]); i >= parseInt(input[0]); i--) { //goes backwards instead to find max
        var sumMAX = 0;
        var length2 = i.toString().length;
        var iString2 = i.toString();
        for (let j = 0; j < parseInt(length2); j++) {
            sumMAX += parseInt(iString2[j]);
        }
        if (sumMAX == parseInt(input[2])) {
            console.log(iString2);
            return;
            }
        }
    }
    
    
    return decipher(input);
    

    Ouput:

    129
    480
    

    I will probably do some tinkering within my code to make it more compact, but I am happy that I found a solution nonetheless. Thank you!