The Code for Eureka!
// Validation on user input
function isValid() {
// Get Values from Page
let startValue = parseInt(document.getElementById('startValue').value);
let endValue = parseInt(document.getElementById('endValue').value);
// Call Empty Table
emptyTable();
// Validates input (makes sure numbers are within rage, start:1-20 | end 21-1,000,000)
if (startValue < 1 || startValue > 20 || endValue < 21 || endValue > 1000000) {
alert('Please enter a correct Start Value (1-20) or End Value (21-1,000,000)');
} else {
calcValues(startValue, endValue);
}
}
// Empty the Table of values on the page
function emptyTable() {
let table = document.getElementById('results');
while (table.firstChild) {
table.removeChild(table.firstChild);
}
}
// Calculate Values
function calcValues(startValue, endValue) {
// Generate squence numbers
let fibNum01 = startValue;
let fibNum02 = startValue;
// Loop Through numbers till endValue
while (fibNum01 <= endValue) {
// Call printValue
printValue(fibNum01);
// Advance numbers in sequence
fibRes = fibNum01 + fibNum02;
fibNum01 = fibNum02;
fibNum02 = fibRes;
}
}
// Output Values to Page
function printValue(resultNum) {
let tr = document.createElement('tr');
let td = document.createElement('td');
let b = document.createElement('b');
// Calls isPrime to give different output if it is a prime number
if (isPrime(resultNum)) {
document.getElementById('results').appendChild(tr).appendChild(td).appendChild(b).innerHTML = resultNum + ' is Prime!';
} else {
document.getElementById('results').appendChild(tr).appendChild(td).innerHTML = resultNum;
}
}
// Checks if number is Prime
function isPrime(num) {
let sum = true;
if (num > 1) {
for (let i = num - 1; i > 1; i--) {
if (num % i === 0) {
sum = false;
break;
}
}
} else {
sum = false;
}
return sum;
}
The Code is structured in the folllowing functions:
isValid
This function checks to see if the user input is valid and within range where the start value must be between 1 and 20 and the end value must be between 21 and 1,000,000. If it isn't there is an alert telling the user to enter the correct numbers. If it is, it calls calcValues passing the user inputs. Also tucked in here is a clearing of any previous tables generated by the user by calling the emptyTable function
emptyTable
This function loops through the table children and deletes each one until the table is empty. Can be called upon by the user from the page as well.
calcValues
This function takes in the user inputs and loops through calling to printValue function on the newly stored first value. It then claculates the next fibbonacci sequence number and stores it in the first value.
printValue
This function creates table row, table data, and bold elements and appends them to the current table checking to see if the value is prime by calling the isPrime function.
isPrime
This function returns a boolean answer to see if the value sent to it is prime by verifying it is only divisable by itself and the number 1.