JavaScript conditional statements and loops - Exercises, Practice, Solution
JavaScript conditional statements and loops [ 12 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts.]
1. Write a JavaScript program that accept two integers and display the larger.Go to the editor
Click me to see the solution
2. Write a JavaScript conditional statement to find the sign of product of three numbers. Display an alert box with the specified sign. Go to the editor
Sample numbers : 3, -7, 2
Output : The sign is -
Click me to see the solution
3. Write a JavaScript conditional statement to sort three numbers. Display an alert box to show the result. Go to the editor
Sample numbers : 0, -1, 4
Output : 4, 0, -1
Click me to see the solution
4. Write a JavaScript conditional statement to find the largest of five numbers. Display an alert box to show the result. Go to the editor
Sample numbers : -5, -2, -6, 0, -1
Output : 0
Click me to see the solution
5. Write a JavaScript for loop that will iterate from 0 to 15. For each iteration, it will check if the current number is odd or even, and display a message to the screen. Go to the editor
Sample Output :
"0 is even"
"1 is odd"
"2 is even"
----------
----------
Click me to see the solution
6. Write a JavaScript program which compute, the average marks of the following students Then, this average is used to determine the corresponding grade. Go to the editor
Student Name | Marks |
---|---|
David | 80 |
Vinoth | 77 |
Divya | 88 |
Ishitha | 95 |
Thomas | 68 |
The grades are computed as follows :
Range | Grade |
---|---|
<60 | F |
<70 | D |
<80 | C |
<90 | B |
<100 | A |
7. Write a JavaScript program which iterates the integers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples
of both three and five print "FizzBuzz". Go to the editor
Click me to see the solution
8. According to Wikipedia a happy number is defined by the following process :
"Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers
for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers)".
Write a JavaScript program to find and print the first 5 happy numbers. Go to the editor
Click me to see the solution
9. Write a JavaScript program to find the armstrong numbers of 3 digits. Go to the editor
Note : An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
Click me to see the solution
10. Write a JavaScript program to construct the following pattern, using a nested for loop. Go to the editor
* * * * * * * * * * * * * * *
11. Write a JavaScript program to compute the greatest common divisor (GCD) of two positive integers. Go to the editor
Click me to see the solution
12. Write a JavaScript program to sum the multiples of 3 and 5 under 1000. Go to the editor
Click me to see the solution
More to Come !
* To run the code mouse over on Result panel and click on 'RERUN' button.*
Live Demo
See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
JavaScript: Tips of the Day
Array of nested arrays
const person = { name: 'Owen', age: 23, }; for (const [x, y] of Object.entries(person)) { console.log(x, y); }
Object.entries(person) returns an array of nested arrays, containing the keys and objects:
[ [ 'name', 'Owen' ], [ 'age', 23 ] ]
Using the for-of loop, we can iterate over each element in the array, the subarrays in this case. We can destructure the subarrays instantly in the for-of loop, using const [x, y]. x is equal to the first element in the subarray, y is equal to the second element in the subarray.
The first subarray is [ "name", "Owen" ], with x equal to "name", and y equal to "Owen", which get logged. The second subarray is [ "age", 23 ], with x equal to "age", and y equal to 23, which get logged.
Ref: https://bit.ly/3jFRBje
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework