Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write, test, and debug (if necessary) JavaScript scripts for the problems that f

ID: 3840406 • Letter: W

Question

Write, test, and debug (if necessary) JavaScript scripts for the problems that follow. When required to write a function, you must include a script to test the function with at least two different data sets. In all cases, for testing, you must write an HTML file that references the JavaScript file.

4.1 Output : A table of the numbers from 5 to 15 and their squares and cubes, using alert .

4.2 Output : The first 20 Fibonacci numbers, which are defined as in the sequence 1, 1, 2, 3, . . . where each number in the sequence after the second is the sum of the two previous numbers. You must use document.write to produce the output.

4.3 Input : Three numbers, using prompt to get each. Output : The largest of the three input numbers. Hint : Use the predefined function Math.max .

4.4 Modify the script of Exercise 4.2 to use prompt to input a number n that is the number of the Fibonacci number required as output.

Explanation / Answer

Hi,

Please find below the answers-

Ans 4.1 Code for squares and cubes-

html>
<head><title>JS Demo</title></head>
<body>
<table border="1">
<tr><th>num</th><th>square</th><th>cube</th>
<tr>
<script>
for(i=5;i<=1;i++)
{
document.write("
<tr><td>"+i+"</td><td>"+(i*i)+"</td><td>"+(i*i*i)+"</td></tr>
");
}
</script>
</body>
</html>

Ans 4.2 -

var looping = function(20) {
var a = 0, b = 1, f = 1;
for(var i = 2; i <= 20; i++) {
f = a + b;
a = b;
b = f;
}
return f;
};

Ans 4.3

function Maxi(num1, num2, num3){
var max = 0;
if((num1 >= num2) && (num1 >= num3)){
max = num1;
}
else if((num2 >= num1) && (num2 >= num3)){
max = num2;
}
else{
max = num3;
}
return max;
}

var arr = [];
for(i=0;i<3;i++){
arr[i] = parseInt(prompt("Enter a number"));
}


document.write(Maxi.apply(this, arr));

Ans 4.4


n = parseInt(prompt("Enter a number"));
var looping = function(n) {
var a = 0, b = 1, f = 1;
for(var i = 2; i <= n; i++) {
f = a + b;
a = b;
b = f;
}
return f;
};