Javascript for Beginners sourceCode-
1. Print 0 to 10 by using for loop and while loop
IF you want to print 0 to 1o in descending order you can use loops .
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function number(){
// for(let i= 1;i<=10;i++){
// document.write("the nember is "+i+"<br>");
// }
let i=1;
while(i<=10){
document.write(`the new while number is ${i} <br> `);
i++;
}
}
number();
</script>
</body>
</html>
2. How to Print number in descending order in javascript?
Source Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function dec(){
for(let i = 10; i>=1;i--){
document.write(`The number is ${i}<br>`)
}
}
dec();
</script>
</body>
</html>
3. Print Odd Number
Source Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function odd(){
for(let i = 1;i<=10;i+=2){
document.write(`Method one with for loop ${i} <br>`);
}
for(let k=1;k<=10;k++) {
if(k%2!==0){
document.write(`Method two with for loop ${k} <br>`);
}
}
//with while loop
let j = 1;
while(j<=10) {
document.write(`Method one with while loop ${j} <br>`);
j+=2;
}
// print 1 to 10 only even number by mod method with for loop
}
odd();
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function odd(){
for(let i = 1;i<=10;i+=2){
document.write(`Method one with for loop ${i} <br>`);
}
for(let k=1;k<=10;k++) {
if(k%2!==0){
document.write(`Method two with for loop ${k} <br>`);
}
}
//with while loop
let j = 1;
while(j<=10) {
document.write(`Method one with while loop ${j} <br>`);
j+=2;
}
// print 1 to 10 only even number by mod method with for loop
}
odd();
</script>
</body>
</html>
4. Print Even Number
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//50 -100 even
function even(){
for(let i= 50;i<=100;i++){
if(i%2==0){
document.write(`Then even number is ${i}<br>`)
}
}
}
even();
</script>
</body>
</html>
5. Print table of 3 by using for loop
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function tab(){
let j= 3
for(let i= 1;i<=10;i++){
document.write(`${j} times ${i} is ${j*i}<br>`)
}
}
tab();
</script>
</body>
</html>
6. How to add or Sum in Javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function total(){
sum=0
let array=[5,3,2,5,1]
for(let i=0;i<array.length;i++){
sum+=array[i]
}
document.write(`Total is ${sum}`);
}
total();
</script>
</body>
</html>
7. Find smallest or Min number in Array in Javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function minimum(){
let array= [5,10,15,2,20,4]
let min=array[0]
for(let i = 0 ;i<array.length;i++){
if(min>array[i]){
min= array[i]
}
}
document.write(`The minimum value is ${min}`)
}
minimum();
</script>
</body>
</html>
8. Find Largest or Max number in Javascript Array
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function maximum(){
let array = [5,4,2,6,10,1]
let max= array[0]
for(let i=0;i<array.length;i++){
if(array[i]>max){
max= array[i]
}
}
document.write(`The largest element in array is ${max}`)
}
maximum();
</script>
</body>
</html>
9. Find Average in Javascript Array
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function avg(){
let sum= 0;
let array= [1,2,3]
for(let i= 0;i<array.length;i++){
sum+=array[i]
}
document.write(`The average is ${sum/array.length}`)
}
avg();
</script>
</body>
</html>