Skip to content

Instantly share code, notes, and snippets.

@cFerg
Created December 28, 2015 07:33
Show Gist options
  • Select an option

  • Save cFerg/cb5d757b4db7560768e5 to your computer and use it in GitHub Desktop.

Select an option

Save cFerg/cb5d757b4db7560768e5 to your computer and use it in GitHub Desktop.
Sum of Range Systems
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="Sum.js"></script>
</head>
<body>
<table>
<tr>
<th>Minimum Number</th>
<th>Maximum Number</th>
</tr>
<tr>
<td><input type="text" id="num1" placeholder="Minimum Number" value="0" oninput="setNum((this.value),(document.getElementById('num2').value));" ></td>
<td><input type="text" id="num2" placeholder="Maximum Number" value="0" oninput="setNum((document.getElementById('num1').value),(this.value));" ></td>
</tr>
</table>
<p id="Sum">Please enter a valid integer - To display the sum of the range.</p>
</body>
</html>
var minNum;
var maxNum;
function setNum(num1,num2){
//The input is a number
if ((/^-?[\d.]+(?:e-?\d+)?$/.test(num1) == true)&&(/^-?[\d.]+(?:e-?\d+)?$/.test(num2) == true)){
//Converts variables to Integer format
minNum = parseInt(num1);
maxNum = parseInt(num2);
//Checks if the variable ordering -> Then rangecheck
orderCheck();
}
//The input isn't a number
else{
document.getElementById("Sum").innerHTML = ("Please enter a valid integer - To display the sum of the range.");
}
}
function orderCheck(){
var tempMax = maxNum;
var tempMin = minNum;
//If numbers are in incorrect order, re-arrange them
if (minNum > maxNum){
maxNum = tempMin;
minNum = tempMax;
//Recheck
orderCheck();
}
//If numbers are correct order, do a range check
else{
sumGet();
}
}
function sumGet(){
//If the highest number is positive
if (maxNum > 0){
//If the lowest number is positive
if (minNum > 0){
//Sets a temporary variable for display
var preMin = minNum;
//Subtracts one for proper range count
minNum = (minNum - 1);
//Gets the range from (minNum) to (maxNum)
var highSum = (((+maxNum/2) * (+maxNum - 1)) + +maxNum);
//Gets the range from zero to (minNum)
var lowSum = (((+minNum/2) * (+minNum - 1)) + +minNum);
//Subtracts the difference outside the range
var sum = (highSum - lowSum)
//Displays the results
document.getElementById("Sum").innerHTML = ("The sum from " + +preMin + " -> " + +maxNum + " = " + +sum);
}
//If the lowest number is negative
else if (minNum < 0){
//Gets the positive range from zero to (maxNum)
var highSum = (((+maxNum/2) * (+maxNum - 1)) + +maxNum);
//Gets the negative range from (minNum) to zero
var lowSum = (((+minNum/(0-2)) * (+minNum + 1)) + +minNum);
//Adds the sums together
var sum = (highSum + lowSum)
//Displays the results
document.getElementById("Sum").innerHTML = ("The sum from " + +minNum + " -> " + +maxNum + " = " + +sum);
}
//If the lowest number is zero
else if (minNum == 0){
//Gets the range from zero to (maxNum)
var sum = (((+maxNum/2) * (+maxNum - 1)) + +maxNum);
//Displays the results
document.getElementById("Sum").innerHTML = ("The sum from 0 -> " + +maxNum + " = " + +sum);
}
}
//If the highest number is negative
else if (maxNum < 0){
//If the lowest number is positive
if (minNum > 0){
//Gets the negative range from (maxNum) to zero
var highSum = (((+minNum/(0-2)) * (+minNum + 1)) + +minNum);
//Gets the positive range from zero to (minNum)
var lowSum = (((+minNum/2) * (+minNum - 1)) + +minNum);
//Adds the sums together
var sum = (highSum + lowSum)
//Displays the results
document.getElementById("Sum").innerHTML = ("The sum from " + +maxNum + " -> " + +minNum + " = " + +sum);
}
//If the lowest number is negative
if (minNum < 0){
//Sets a temporary variable for display
var preMax = maxNum;
//Adds one for proper range count
maxNum = (maxNum + 1);
//Gets the negative range from zero to (maxNum)
var highSum = (((+maxNum/(0-2)) * (+maxNum + 1)) + +maxNum);
//Gets the negative range from zero to (minNum)
var lowSum = (((+minNum/(0-2)) * (+minNum + 1)) + +minNum);
//Gets the range from (minNum) to (maxNum)
var sum = (lowSum - highSum);
//Displays the results
document.getElementById("Sum").innerHTML = ("The sum from " + +minNum + " -> " + +preMax + " = " + +sum);
}
//If the lowest number is zero
else if (minNum == 0){
//Gets the negative range from (maxNum) to zero
var sum = (((+maxNum/(0-2)) * (+maxNum + 1)) + +maxNum);
//Displays the results
document.getElementById("Sum").innerHTML = ("The sum from " + +maxNum + " -> 0 = " + +sum);
}
}
//If the highest number is zero
else if (maxNum == 0){
//If the lowest number is positive
if (minNum > 0){
//Gets the negative range from zero to (minNum)
var sum = (((+minNum/2) * (+minNum - 1)) + +minNum);
//Displays the results
document.getElementById("Sum").innerHTML = ("The sum from 0 -> " + +minNum + " = " + +sum);
}
//If the lowest number is negative
else if (minNum < 0){
//Gets the negative range from zero to (minNum)
var sum = (((+minNum/(0-2)) * (+minNum + 1)) + +minNum);
//Displays the results
document.getElementById("Sum").innerHTML = ("The sum from " + +minNum + " -> 0 = " + +sum);
}
//If the lowest number is zero
else if (minNum == 0){
//Displays the results
document.getElementById("Sum").innerHTML = ("The sum from 0 -> 0 = 0");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment