It needs to still return the correct row in the case of a input number that doesn't make a full pyramid.
Inputs old, Internal, lr are all internal and shouldn't be required to fill in.
const pir = (Input, old, Internal, lr) => {
console.log({
Input, Internal, lr
});
if (Internal === undefined || lr === undefined || old === undefined) {
Internal = 1;
lr = 1;
old = 1;
}
console.log({
Input, Internal, lr
});
if ( ( Input === Internal ) || ( Input < Internal && old > Internal ) ) {
return lr;
} else {
return pir(Input, Internal, Internal + (lr + 1), lr + 1)
}
};
The condition old > internal is never going to be true.
You need to get the condition where there is an overrun only (not equality). When there is an overrun, return lr - 1, which is the previous value of lr, and which was OK:
if (Input < Internal) {
return lr - 1; // Return previous value of lr (the last successful one)
}
Some other remarks:
old argumentSo here is how the function could look:
const pir = (input, internal=1, lr=1) => {
if (input < internal) {
return lr - 1; // Return previous value of lr (the last successful one)
} else {
return pir(input, internal + lr + 1, lr + 1)
}
};
Finally, the relationship between the size and the width of such a "pyramid" is a mathematical one. This relationship can be solved in terms of the width, and so we can write pir as:
const pir = input => Math.floor((Math.sqrt(1+8*input) -1)/2);