Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <input type="text" id="number-input" maxlength="4" title="title" placeholder="Enter any four digit number"><button id="generate-btn" disabled>Generate!</button>
  	<br><br>
  	<div id="display"></div>
              
            
!

CSS

              
                body {
  padding: 1rem;
  font-family: arial;
}
#number-input {
  padding: 10px;
  border-radius: 4px 0 0 4px;
  border: 1px solid #ccc;
}
#generate-btn {
  padding: 10px;
  color: #f4f4f4;
  background: #333333;
  border: 0;
  cursor: pointer;
}
#generate-btn:disabled {
  opacity: 0.4;
}

span.hl {
  display: block;
  margin: .5rem 0;
  font-size: 1.2rem;
}

div {
  margin-bottom: .5rem;
}

              
            
!

JS

              
                /*
 	Init DOM Elements
*/
const input = document.querySelector("#number-input");
const generateBtn = document.querySelector("#generate-btn");
const display = document.querySelector("#display");

// init global variable for calculations
let largest, smallest, result, counter = 1;

// register event to validate input value
input.addEventListener("keyup", e => {
	const regex = /^[0-9]{4}$/
	if(regex.test(input.value)) {
		generateBtn.removeAttribute("disabled");
	} else 
		generateBtn.setAttribute("disabled", "");
});

// register event to handle calculations
generateBtn.addEventListener("click", e => {

	// set input to readonly until constant is generated
	input.setAttribute("readonly", "");
	// shuffle input value
	result = shuffle(input.value);
	// init output for insertion
	let output = "";
	// append first result to output
	output += `<div>${largest} - ${smallest} = ${result}</div>`;

	// keep running shuffle function with each generated result
	// until constant is reached
	while(result !== 6174) {
		counter++; // incremen counter for every iteration
		shuffle(String(result)); // get new shuffled digit
		// append for DOM entry
		output += `<div>${largest} - ${smallest} = ${result}</div>`;
	}

	// update the DOM
	display.innerHTML = `<span class="hl">Number - ${input.value}, Steps - ${counter}</span>${output}`
	// remove readonly attribute (i.e constant generated)
	input.removeAttribute("readonly", ""); 
	counter = 1; // reset counter
});

/*
	* Takes in a string and splits it into an array 
	* Generates two numbers based off of array in ASC and DESC order
	* return the diff between the two numbers
*/
function shuffle(str) {
	let strArray = str.split("");
	largest  = strArray.sort((a,b) => b - a).join("");
	smallest = strArray.slice().sort((a,b) => a - b).join("");
	result   = largest - smallest;

	return result;
}

              
            
!
999px

Console