Alright, I've decided to clean up my code after realizing how bad it was. Here's what I came up with to correctly retrieve the value you requested.
IMPORTANT: Keep in mind that this solution is designed to work only for values between 0 and 1:
window.onload = ()=>{
function getLen(num){
let currentNumb = num;
let integratedArray = [];
let realLen = 0;
/* While the number is not an integer, we will multiply a copy of the original
* value by ten. When the loop detects that the number is already an integer,
* it breaks, storing each transformation of the number in the integratedArray */
while(!(Number.isInteger(currentNumb))){
currentNumb *= 10;
integratedArray.push(currentNumb);
}
/* We iterate over the array and compare each value with a specific operation.
If the resulting value equals the current item in the array, we update realLen */
for(let i = 0; i < integratedArray.length; i++){
if(Math.floor(integratedArray[i]) === Math.floor(num * Math.pow(10, i + 1))){
realLen = i;
}else{
break;
}
}
return realLen;
}
// Get the float value of a number between 0 and 1 as an integer
function getShiftedNumber(num){
// Obtain the length to convert the float part of the number to an integer
const len = getLen(num);
/* Multiply the number by (10) ^ length, eliminating the decimal point and transforming it
into an integer */
return num * (Math.pow(10, len));
}
console.log(getShiftedNumber(0.84729347293923));
}
Here's a breakdown of the process:
To convert the number without using strings or regex, we first need to determine its length. This is challenging without string conversions, so I created the function getLen to address this issue.
The getLen function contains 3 variables:
- currentNumb: A copy of the original number used to find its length through transformations without altering the original reference.
We repeatedly multiply this value until it becomes an integer, allowing us to apply further transformations using a while loop.
NOTE: The term "Fake Integer" refers to additional digits unintentionally added during testing sessions. Hence, filtering out these "trash numbers" becomes crucial for processing them later on.
- integratedArray: Stores the results of initial operations until the last number stored is an integer, however, one of these integers is considered fake. It helps identify discrepancies when compared to the original value multiplied by (10 * i + 1).
For example, the first 12 values are identical, but beyond that point, differences emerge. These discrepancies indicate the presence of unwanted numbers within the sequence.
- realLen: Serves as storage for the final length of the number after converting its float component into an integer.