Question

In: Computer Science

Activity 11: JS-Loops and Functions Task 1: Use JavaScript for loop to print the first 5...


Activity 11: JS-Loops and Functions
Task 1:
Use JavaScript for loop to print the first 5 non-zero integers on each line.
Task 2:
Use JavaScript while loop to print the first 5 non-zero integers on each line.
Task 3:
Use JavaScript for loop to print the first 5 even integers on each line.
Task 4:
Write a JavaScript function to multiple two numbers and return the result.
Task 5:
Write a JavaScript factorial that asks users to enter a positive and return the result.
i) Without using recursion ii) Using recursion Task 6:
Use a nested for or while loop to produce a table below.
1,1 1,2 1,3 1,4 1,5
2,1 2,2 2,3 2,4 2,5
3,1 3,2 3,3 3,4 3,5
4,1 4,2 4,3 4,4 4,5
5,1 5,2 5,3 5,4 5,5


Solutions

Expert Solution

1) var i;
for (i = 0; i <= 5; i++) {
console.log(i + 1);
}

2)

var i = 0;

while (i <= 5) {
console.log(i + 1);
i++;
}

3)

for (i = 1; i <= 10; i++) {

if(i % 2 == 0)

{

console.log(i);

}

}

4)

function prod(p1, p2) {
  return p1 * p2;   // The function returns the product of p1 and p2
}

5)

//Using recursion

function factorial(n) {

if(n == 0)

{

return 1;

}
  return n * factorial(n - 1);
}

//Without recursion

function factorial(n) {

var prod = 1;

for(i = 1;i <= n;++i)

{

prod *= i;

}

return prod;
}

6)

var i = 1;

var j = 1;

var text = "";

for(i = 1;i <= 5;i++)

{

text = "";

for(j = 1;j <= 5;j++)

{

text += " " + i + "," + j + " ";

}

console.log(text);

}

OUTPUT:


Related Solutions

After reading chapter 5 of the textbook and watching the video “JavaScript Loops” JavaScript Loops and...
After reading chapter 5 of the textbook and watching the video “JavaScript Loops” JavaScript Loops and “JavaScript Decision Statements” 11 JavaScript Decision Statements Create a html web page that allows the coach of a town’s soccer league to enter the ages of all the children who have registered for soccer this year. Children between the ages of 4 and 15 are allowed to sign up for soccer but there can be a lot of variation in the number of children...
Solve for number 2, Use Functional Programming ONLY, so No For loops Use JavaScript: Start with...
Solve for number 2, Use Functional Programming ONLY, so No For loops Use JavaScript: Start with an array called inputtable. The array should have numbers between 1 and 10. NOTE: Do NOT use a form of a ‘for’ loop anywhere, including iterators. This is meant to be a functional exercise. Get the odd multiples of 5 between 1 and 100. 5, 15, …
C++ Code Using all for loops 1. Print 5 lines with 1 asterisk per line 2....
C++ Code Using all for loops 1. Print 5 lines with 1 asterisk per line 2. Print 5 asterisk on 1 line. Endl at the end of the line. 3. Ask for a positive # and print that many asterik on the next line. 4. Using 2 for loops one inside of another that only prints 1 asterik, print a hill shaped triangle: Ex( Input a number a: 4 * ** *** **** 5. Change the for statements to print...
class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers...
class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers from low to high for(int i = low; i <= high; i++){ System.out.println(i); } } public int sumOfNumbers(int n){ // using a for loop, calculate and return the sum of first n numbers // i.e n = 5, answer = 5+4+3+2+1 = 15 int sum = 0; for(int i = 1; i <= n; i++){ sum += i; } return sum; } public void...
-create a magic 8 ball program in JavaScript - use a loop to ask for the...
-create a magic 8 ball program in JavaScript - use a loop to ask for the question - use a random number to get the answer let randAnswer=Math.round(Math.round()*10); - must have at least 10 different answers -Must use either elseif or switch statement for answer -must output both answer and user input to console - the program should repeat indefinitely until either blank input, or cancel is selected Bug in jsbin.com Please don't use HTML thanks
PLEASE NOTE:1)-DO NOT USE FUNCTIONS USE ONLY DO WHILE LOOP.                          2)DO NOT USE IN-BUILT FUNCTIONS....
PLEASE NOTE:1)-DO NOT USE FUNCTIONS USE ONLY DO WHILE LOOP.                          2)DO NOT USE IN-BUILT FUNCTIONS.                          3)Use of string and char is not allowed.             Write a program in c laungage that prints a table of the binary, octal and hexadecimal equivalents of the decimal numbers in the range1 through 256.
JavaScript 1. FizzBuzz Submit js file with functioning code Write a program that uses console.log to...
JavaScript 1. FizzBuzz Submit js file with functioning code Write a program that uses console.log to print all the numbers from 1 to 120, with two exceptions. For numbers divisible by 4, print "Fizz" instead of the number, and for numbers divisible by 10 (and not 4), print "Buzz" instead. When you have that working, modify your program to print "FizzBuzz", for numbers that are divisible by both 4 and 10 (and still print "Fizz" or "Buzz" for numbers divisible...
JavaScript Task Load three images of colorful fall leaves and using functions with parameters do the...
JavaScript Task Load three images of colorful fall leaves and using functions with parameters do the following. (Remember you can call the same function multiple times sending the function different values.) When you move the mouse over the first image, have the second image change to a pumpkin. When you move the mouse off of the first image, have the second image change back to the original leaves. When you move the mouse over the second image, have the third...
In MATLAB FOR loops: Repeat a block command a certain number of times Use a loop...
In MATLAB FOR loops: Repeat a block command a certain number of times Use a loop index Can use a non-unit stride Are required for any kind of animations An accumulator: MUST start at zero Can be used to keep track of a quantity each time through the loop Can be used to calculate how a sum changes with each loop Should be coded, for loops are more efficient For loops: Perform a set task a set number of times...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT