Developing Web Applications - JavaScript Follow the instructions bellow for a go
ID: 3876332 • Letter: D
Question
Developing Web Applications - JavaScript
Follow the instructions bellow for a good rate
Just use javascript, no need to include html
Create a program that will display the result of three functions:
a) sum the elements from the Fibonacci sequence
b) divide them by randomly generated number
c) calculate factorial from the rounded result of previous operation
Functions should be stored in one, separate file. The main program should run from the app.js. Finally, a user should provide input for two variables - the number of Fibonacci items and the number responsible for the range of the randomly generated number from point b)
-----------------------------------------------------------------
Code for app.js is showen bellow:
app.js:
function calculateFibonacci(num) {
var a = 1,
b = 0,
temp;
while (num >= 0) {
temp = a;
a = a + b;
b = temp;
num--;
}
return b;
}
function displayFibonacci(s) {
var result = "";
for (i = s; i > 0; i--) {
result += calculateFibonacci(i) + ", ";
}
return result.substring(0, result.length - 2);
}
console.log(displayFibonacci(21));
--------------------------------------------------------------------
Output in Console:
17711, 10946, 6765, 4181, 2584, 1597, 987, 610, 377, 233, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1
[Finished in 0.9s]
Explanation / Answer
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function fib() {
var length = document.getElementById("fabonaci").value;
var randomNo = document.getElementById("random").value;
var sum2=0;
var z=0;
var fibArr = [],
i = 0,
j = 1;
fibArr.push(i);
fibArr.push(j);
while (fibArr.length <= length) {
fibArr.push(fibArr[j] + fibArr[i]);
j++;
i++;
}
for(var z in fibArr)
{
sum2=sum2+fibArr[z];
}
document.write("Fabonaci Series is:-->"+fibArr)
document.write("<br>");
document.write("sum of the series is:-->"+sum2)
document.write("<br>");
var randomNumber=randomfun(randomNo);
document.write("Random number generated is-->"+randomNumber)
document.write("<br>");
var Result=sum2/randomNumber
var roundedResult=Math.round(Result)
document.write("<br>");
document.write("Rounded result(Sum of fabonaci/Random Number)-->"+roundedResult)
document.write("<br>");
var output2=fact(roundedResult)
document.write("Final Result i.e factorail of Rounded Result -->"+output2)
document.write("<br>");
return fibArr;
};
function randomfun(sum2){
var num=Math.floor(Math.random()*sum2);
return num
};
function fact(n){
var i, fact;
fact=1;
for(i=1; i<=n; i++)
{
fact= fact*i;
}
return fact;
};
</script>
</head>
<body>
<form action="#">
<pre>Enter_Fibonacci_sequence:-<input type="number" name="fabonaci" id="fabonaci"><br></pre>
<pre>Enter_Range_of_RandomNo :-<input type="number" name="random" id="random"><br></pre>
<input type="submit" name="submit"><br>
</form>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.