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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                
<body> 
	<div id= "header">
	<h1> Factoring Trinomials Practice </h1>
	</div>
<div class = "box" id = "QuestionBox">

</div>
<button onClick = "bothForms()" > Get Question </button> 

<div class = "box" id = "AnswerBox">

</div>
<button onClick = "getAnswer()"> Reveal Answer </button>
	
</body>
              
            
!

CSS

              
                #header{
	background: #2db22d;
	padding: 1px;
	border: 3px solid #005300;
	border-radius: 5px;
}

body{
	font-family: 'Josefin Slab', serif;
}
h1{
	font-family: 'Raleway', sans-serif;
	text-align: center;
	color: #fff;
	text-shadow: 3px 3px #000;
	
}
.box{
	height: 50px;
	width: 80%; 
	border: 3px solid #005e00;
	border-radius: 5px;
	text-align: left;
	font-size: 24px;
	margin-left: auto;
	margin-right: auto;
	margin-top: 15px;
	padding: 15px;
	}
button {
	background: #ff8d8d;
	padding: 5px;
	border-radius: 5px;
}

@media only screen and (min-width: 768px){
	h1{ 
	font-size: 48px;}
}

.box{
	font-size: 32px;
	text-align: center;
}
              
            
!

JS

              
                var answer = 'Click "Get Question"';
var trinomial = {"a": "", "b": "", "c": ""};
var factorOut = 1;

/* Check if a number has a given factor */
function isFactor(num, fact){
	if(num % fact == 0){
		return true;
	}
	else{
		return false;
	}
}

/* List the factors of a number. */
function listFactors(number){
	factors = [1];
	var i = 2;
	while(i <= number){
		if(isFactor(number, i)){
			factors.unshift(i);
		}
		i++;
	}
	return factors;		
}


/* Find the GCF (greatest common factor) of a list of numbers. */
function GCF(intList){
	var GCF = 1;
	var factorsOfEach = [];
	for(item in intList){
		var num = intList[item];
		var factors = listFactors(num);
		factorsOfEach.push(factors);
	}
	var count = 0;
	factorsOfFirst = factorsOfEach[0];
	var length = factorsOfFirst.length;
	while(count < length){
		var toTest = factorsOfFirst[count];
		var passTest = factorsOfEach.every(arrayContains);
		if(passTest){
			GCF = toTest;
			return GCF;
		}
		else{
			count += 1;
		}
	}
	return GCF
}

var toTest = 1;

/*Check whether an array contains "toTest" */
function arrayContains(array){
	if(array.indexOf(toTest) != -1){
		return true}
	else{
	return false}
}

/* Decide whether to make a number negative. */
function makeNegative() {
	var k = Math.random();
	if (k >= 0.5) { 
		return false; // positive
	} else {
		return true; // negative
	}
}


/* Pick two random positive or negative numbers to be the coefficient and the constant in one factor */
function getBinomial() {
	var coefficient = Math.floor(2 * Math.random() + 1); //Random whole number from 1 to 3
	if(makeNegative()){
		coefficient *= -1;
	}
	var constant = Math.floor(9 * Math.random() + 1); //Random whole number from 1 to 10
	if(makeNegative()){
		constant *= -1;
	}
	if (coefficient < 0){ //If the coefficient is negative, factour out -1
		factorOut *= -1;
		constant /= -1;
		coefficient /= -1;
	}
	var both = [coefficient, constant];
	if(GCF(both) > 1){
		factorOut *= GCF(both);
		both[0] /= GCF(both);
		both[1] /= GCF(both);
	}
	return both;
}


/* Display a binomial factor as a string */
function displayFactor(factor) {
	if(factor[0] != 1){
		return "(" + factor[0] + "x + " + factor[1] + ")"
	}
	else{
		return "(x + " + factor[1] + ")"	
	}
}


/* Multiply the two binomials and return the product as a string */
function expand(factor1, factor2) {
	trinomial.a = factor1[0] * factor2[0] * factorOut;
	trinomial.b = factorOut * (factor1[0] * factor2[1] + factor1[1] * factor2[0]);
	trinomial.c = factorOut * (factor1[1] * factor2[1]);
	var firstTerm = "";
	var secondTerm = "";
	var thirdTerm = "";
	if(trinomial.a == 1){
		firstTerm = "x" + "2".sup() + " + "}
	else{
		firstTerm = trinomial.a + "x" + "2".sup() + " + ";
	}
	if(trinomial.b != 0){
		secondTerm = trinomial.b + "x + ";
	}
	thirdTerm = trinomial.c;
	return firstTerm + secondTerm + thirdTerm;
}


/* Create 2 binomial factors and find their expanded form. Display the expanded form as the question.*/
function bothForms() {
	factorOut = 1;
	answer = "";
	var firstFactor = getBinomial();
	var secondFactor = getBinomial();
	var afterDistr = expand(firstFactor, secondFactor);
	if(factorOut != 1){
		answer += factorOut}
	answer += displayFactor(firstFactor) + displayFactor(secondFactor);
	$("#QuestionBox").html(afterDistr);
	$("#AnswerBox").html("");
}

/* Display the factored form of the quadratic as the answer */
function getAnswer() {
	$("#AnswerBox").html(answer);
}
              
            
!
999px

Console