Javascript Help - selfDividingNumbers Algorithm that creates all 0

Greeting Stack Overflow!

Firstly, this is my first question!

I am trying to solve the selfDividingNumbers algorithm, and I came across this interesting problem. This function must take a series of numbers to check if they are divisible by themselves.

Self-separation example:

128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. 

My attempt with Javascript.

 /* selfDividingNumbers( 1, 22 ); */ var selfDividingNumbers = function(left, right) { var output = []; while(left <= right){ // convert number into an array of strings, size 1 var leftString = left.toString().split(); // initialize digit iterator var currentDigit = leftString[0]; for(var i = 0; i < leftString.length; i++){ currentDigit = parseInt(leftString[i]) console.log( left % currentDigit ); } // increment lower bound left++; } return output }; 

When comparing the current lower bound with the current lower left digit of % currentDigit, it always produces zero! I suppose this is probably a type error, but I'm not sure why and would like someone to point out why!

I would also like to see any other ideas to avoid this problem!

I thought this was a good chance to better understand Javascript, given that I do not know why my program produces this output. Any help would be appreciated! :)

Thanks, stack overflow!

+5
source share
3 answers

The split() call doesn't buy you anything. Delete it and you will get the expected results. You still have to write code to populate output , though.

+2
source

@Joseph's answer may fix your current code, but I think there is a potentially easier way to do this. Consider the following script:

 var start = 128; var num = start; var sd = true; while (num > 0) { var last = num % 10; if (start % last != 0) { sd = false; break; } num = Math.floor(num / 10); } if (sd) { print("Is self dividing"); } else { print("Is NOT self dividing"); } 

Demo

To check each digit in a number for its ability to cleanly separate the original number, you can simply use a loop. At each iteration, check num % 10 to get the current digit, and then divide the number by ten. If we never see a number that cannot be divided evenly, then the number does not divide itself, otherwise it is.

+2
source

So, the string split method takes a string and returns an array of string parts. The method expects a parameter, however, a separator element. If no delimiter is provided, the method returns only one part, the string itself. In your case, you probably planned to split the string into separate characters, which would mean that the divider would be an empty string:

 var leftString = left.toString().split(''); 

Since you are already familiar with console.log , note that you can also use it to debug your program. If you get confused in the output of left % currentDigit , one thing you could try is to register the variables just before the call,

 console.log(typeof left, left, typeof currentDigit, currentDigit) 

which can give you an idea of ​​where to look next.

+1
source

Source: https://habr.com/ru/post/1275840/


All Articles