JaneDoe
JaneDoe

Reputation: 39

Why with while .join() gives me error and without it works

So, I have this code, I'm just trying to do a reverse thing with js. I introduce a number and it sorts itself from max to lower and lower to max, with two variables max and min. I substract the max to the min, and it repeats until the result is 6174.

I don't know why when I put all this in a while max.join() gives me an error: "Uncaught TypeError: max.join is not a function at <'anonymous>:28:17"

But without the while it works.

	var num = prompt('Introduzca un número de 4 digitos de al menos 2 digitos distintos', '1234');

	var output = [],
		sNum = num.toString(),
		n = 0,
		max = [],
	    min = [],
	    a = 0,
	    kap= 6174,
	    v = 1;
  
  	while (a != kap) {
      for (var i = 0, len = sNum.length; i < len; i++) {
        output.push(+sNum.charAt(i));
      }

      min = output.sort((a, b) => a - b );

      var x=3;

      for (var c = 0; c < 4; c++) {
        max[c] = min[x];
        x--;
      }

      min = min.join('');
      max = max.join('');
      sNum = parseInt(max)-parseInt(min);
      
      if (sNum == kap) {
      	a = kap;
      }
      else {
      	a = 0;
        v++;
      }
   }
   
   console.log(v);
   console.log(a);

	var num = prompt('Introduzca un número de 4 digitos de al menos 2 digitos distintos', '1234');

	var output = [],
		sNum = num.toString(),
		n = 0,
		max = [],
	    min = [],
	    a = 0,
	    kap= 6174,
	    v = 1;
  
      for (var i = 0, len = sNum.length; i < len; i++) {
        output.push(+sNum.charAt(i));
      }

      min = output.sort((a, b) => a - b );

      var x=3;

      for (var c = 0; c < 4; c++) {
        max[c] = min[x];
        x--;
      }

      min = min.join('');
      max = max.join('');
      sNum = parseInt(max)-parseInt(min);
   
   console.log(max);
   console.log(min);
   console.log(sNum);

Thank you so much, I finally saw the problem and now I have the code complete :)

	var num = prompt('Introduzca un número de 4 digitos de al menos 2 digitos distintos', '1234');

	var	sNum = num.toString(),
		n = 0,	
		a = 0,
		kap= 6174,
		v = 1;

	if (num == (1111 || 2222 || 3333 || 4444 || 5555 || 6666 || 7777 || 8888 || 9999 || 0000 || 0)) {
		console.log(8);
	}

	else if (num == 6174) {
		console.log(0);
	}

	else if (sNum.length < 4) {
		while (sNum.length < 4) {
			sNum = ('0' + sNum);
		}
		if (sNum == "0000") {
			console.log(8);
		}
		else {
			calculoKaprekar();
		}
	}

	else {
		calculoKaprekar();
	}

  	function calculoKaprekar () {
	  	while (a != kap) {
	  		var output = [];

	  		sNum = sNum.toString();

			for (var i = 0, len = sNum.length; i < len; i++) {
				output.push(+sNum.charAt(i));
			}
		
			var max = [],
				min = [];

			min = output.sort((a, b) => a - b );

			var x=3;

			for (var c = 0; c < 4; c++) {
				max[c] = min[x];
				x--;
			}

			min = min.join('');
			max = max.join('');
			sNum = parseInt(max)-parseInt(min);

			if (sNum == kap) {
				a = kap;
			}
			else {
				v++;
			}
	   }
	   console.log(v);
	}

Upvotes: 0

Views: 66

Answers (1)

skovy
skovy

Reputation: 5650

The first iteration max is an array as you expect:

[
  4,
  3,
  2,
  1
]

However, within that first iteration the value is being reassigned:

max = max.join('');

So on the second iteration, it is now "4321" (a string).

And "4321".join("") is not a function since join is not a function on strings.

Upvotes: 1

Related Questions