Home > Mobile >  I cannot seem to have the numbers decimals removed from the array
I cannot seem to have the numbers decimals removed from the array

Time:11-21

This might be pretty basic, but my code seems to have a few errors or expectations that might not work for me. Some background information: My code was created so that it asks the user for an integer. Then the user's input will be analyzed and will show the user an output of numbers that multiplied together will equal the user's input.

Expectation

Input: 7 Output(In console): (1,7)(7,1).

But instead, my code also inputs the decimals that equal the user's input.

Reality

Input: 7 Output: (1.1666666666666667, 6), (1.4, 5), (1.75, 4), (2.3333333333333335, 3), (3.5, 2), (7, 1), (Infinity, 0).

     var numInput= parseInt(prompt("Please enter a number larger than 1"));
        //This asks the user for the input 
    var valueArray = [];//This is the empty array we are going to use in the further code. 
            if(numInput <= 0 || numInput <= 1) {
                 console.log("Goodbye!")
        }//This line is an if loop which if the user inputs 0 or 1 then the code will end. 
   
        while(numInput > 0) {// This while loop is there so that the user can input as many numbers he wants 
         var valueArray = [];//Now the numbers are inside this empty array
         var numInput = parseInt(prompt("Please enter a number larger than 1"));
   
         for (var iterator = 0; iterator < numInput;   iterator) {//This for loop is the calculation, for when a = 1, the a has a greater value then 
           var valueSubtracted = numInput / iterator //This is where the variable subtracts the orignal value n so that we have something along the lines of (1,6) instead (1,7)
           valueArray.unshift(valueSubtracted   ", "   iterator ); //This just moves the answers into a concantination, and moves into the array
           }
        
     
        console.log("The additive combinations are: "   "("   valueArray.join("), (")   "). ");
       }  
         

All I really want is for the decimals to be removed and the number associated with it. For example:

Input: 7 Output: (1.1666666666666667, 6), (1.4, 5), (1.75, 4), (2.3333333333333335, 3), (3.5, 2), (7, 1), (Infinity, 0).

Note that above: the bolded and italicized are supposed to be removed from the array. This is what it looks like in the console.

CodePudding user response:

You should check if it's divisiable by the integer. To do so, use n % m === 0 and check it in if-clause. It should be created inside for-loop. Also: Doing zero division will not cause error. Instead it, it returns Infinity. Consider change start value to 1 in the for-loop to avoid that.

CodePudding user response:

Since I was so "gracefully" downvoted for being nice, here's a better answer.

Code:

while (true) {
    const numInput = parseInt(prompt("Please enter a number larger than 1").replace(/\D/g, ""));
    if (numInput > 1) {
        const valueArray = [];
        for (let iterator = 1; iterator <= numInput; iterator  ) {
            const valueDivided = ((numInput / iterator) % 1 === 0) ? (numInput / iterator) : false
            if (valueDivided) valueArray.unshift(`(${valueDivided}, ${iterator})`);
        }
        console.log(`The additive combinations are: ${valueArray.join(",")}`)
        continue;
    }
    console.log("Goodbye!")
}

Breakdown:

1- I used const and let instead of var

2-Infinite loop:

while (true) // will keep repeating the same process until user clicks "Cancel"

3- Clean before parsing

const numInput = parseInt(prompt("Please enter a number larger than 1").replace(/\D/g, ""));
// uses RegEx to remove any non-numeric values
// parseInt() to get integer from prompt text

if the number is not larger than 1 console.log("Goodbye!")

4- used let instead of var in the for loop and replace iterator with iterator ... the difference is that iterator first increments by 1 THEN the variable can be used. Also, to answer your second comment on my first answer, it only logged (1,7) and not (7,1) because you had iterator < numInput so the loop couldn't reach 7, it stopped at 6. I changed it to iterator <= numInput instead.

5- calculate the possible divisions:

const valueDivided = ((numInput / iterator) % 1 === 0) ? (numInput / iterator) : false
// if the value is a normal number return it, else return false

6-

// only add to the array if truthy
if (valueDivided) valueArray.unshift(`(${valueDivided}, ${iterator})`)

// Normal numbers with the exception of 0 are truthy values, which means if(1) returns true... if(0) returns false
// I used template literals `${dynamic} text ${dynamic}`
// so any added value will be like: (1,7) for example

7- The result is then joined using .join like you used it in the original post

8- continue tells the while loop to move to continue its work. Think of it as return for functions.

CodePudding user response:

Well there's a lot to be improved/corrected in your code, but I'll just focus on making it work like you want it to. Just change the following lines:

var valueSubtracted = ((numInput / iterator) % 1 === 0) ? (numInput / iterator) : Infinity 
// check if valueSubtracted is an N group number or else set it as Infinity

if(isFinite(valueSubtracted)) valueArray.unshift(valueSubtracted   ", "   iterator )
// only add to array if finite number

Also, numInput<=1 is already nimInput<=0, so why have both?

  • Related