0% found this document useful (0 votes)
17 views39 pages

Javascript Coding Exercises Coding For Beginners

The document contains a series of JavaScript coding exercises aimed at beginners, covering various tasks such as displaying the current date and time, checking for leap years, and manipulating strings. Each exercise includes HTML and JavaScript code snippets to demonstrate the solutions. The exercises are designed to help learners practice fundamental programming concepts and syntax in JavaScript.

Uploaded by

SUTARIYA TULASI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views39 pages

Javascript Coding Exercises Coding For Beginners

The document contains a series of JavaScript coding exercises aimed at beginners, covering various tasks such as displaying the current date and time, checking for leap years, and manipulating strings. Each exercise includes HTML and JavaScript code snippets to demonstrate the solutions. The exercises are designed to help learners practice fundamental programming concepts and syntax in JavaScript.

Uploaded by

SUTARIYA TULASI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVASCRIPT

CODING EXERCISES

CODING FOR BEGINNERS


JJ TAM
DISPLAY THE CURRENT DAY AND TIME
PRINT THE CONTENTS
DISPLAY THE CURRENT DATE
FIND THE AREA OF A TRIANGLE
CHECK WHETHER A GIVEN YEAR IS A LEAP YEAR
CALCULATE MULTIPLICATION AND DIVISION
CONVERT TEMPERATURES
FIND THE LARGEST
REVERSE A GIVEN STRING
REPLACE EVERY CHARACTER
CAPITALIZE THE FIRST LETTER
CONVERT NUMBER TO HOURS AND MINUTES
COUNT VOWELS IN A GIVEN STRING
CREATE A NEW STRING
CONCATENATE TWO STRINGS
MOVE LAST THREE CHARACTER
COMPUTE THE SUM
ADD TWO DIGITS
CHECK WHETHER A GIVEN YEAR IS A LEAP
CHECK GIVEN POSITIVE NUMBER
CHECK A STRING STARTS WITH 'JAVA'
CHECK TWO GIVEN INTEGER VALUES
FIND A VALUE WHICH IS NEAREST TO 100
Display the current day and time
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript current day and time</title>
</head>
<body></body>
</html>

JAVASCRIPT CODE
var today = new Date();
var day = today.getDay();
var daylist = ["Sunday","Monday","Tuesday","Wednesday
","Thursday","Friday","Saturday"];
console.log("Today is : " + daylist[day] + ".");
var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();
var prepand = (hour >= 12)? " PM ":" AM ";
hour = (hour >= 12)? hour - 12: hour;
if (hour===0 && prepand===' PM ')
{
if (minute===0 && second===0)
{
hour=12;
prepand=' Noon';
}
else
{
hour=12;
prepand=' PM';
}
}
if (hour===0 && prepand===' AM ')
{
if (minute===0 && second===0)
{
hour=12;
prepand=' Midnight';
}
else
{
hour=12;
prepand=' AM';
}
}
console.log("Current Time : "+hour + prepand + " : " + minute + " : " + second);
Print the contents
of the current window
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Print the current page.</title>
</head>
<body>
<p></p>
<p>Click the button to print the current page.</p>
<button onclick="print_current_page()">Print this page</button>
</body>
</html>

JAVASCRIPT CODE
function print_current_page()
{
window.print();
}
Display the current date
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Write a JavaScript program to get the current date.</title>
</head>
<body>
</body>
</html>

JAVASCRIPT CODE
var today = new Date();
var dd = today.getDate();

var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10)
{
dd='0'+dd;
}

if(mm<10)
{
mm='0'+mm;
}
today = mm+'-'+dd+'-'+yyyy;
console.log(today);
today = mm+'/'+dd+'/'+yyyy;
console.log(today);
today = dd+'-'+mm+'-'+yyyy;
console.log(today);
today = dd+'/'+mm+'/'+yyyy;
console.log(today);
Find the area of a triangle
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>The area of a triangle</title>
</head>
<body>
</body>
</html>

JAVASCRIPT CODE
var side1 = 5;
var side2 = 6;
var side3 = 7;
var s = (side1 + side2 + side3)/2;
var area = Math.sqrt(s*((s-side1)*(s-side2)*(s-side3)));
console.log(area);
Check whether a given year is a leap
year
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Find Leap Year</title>
</head>
<body>
</body>
</html>
JAVASCRIT CODE
function leapyear(year)
{
return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);
}
console.log(leapyear(2016));
console.log(leapyear(2000));
console.log(leapyear(1700));
console.log(leapyear(1800));
console.log(leapyear(100));

OUTPUT
true
true
false
false
false
Calculate multiplication and division
of two numbers
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JavaScript program to calculate multiplication and division of two
numbers </title>
<style type="text/css">
body {margin: 30px;}
</style>
</head>
<body>
<form>
1st Number : <input type="text" id="firstNumber" /><br>
2nd Number: <input type="text" id="secondNumber" /><br>
<input type="button" onClick="multiplyBy()" Value="Multiply" />
<input type="button" onClick="divideBy()" Value="Divide" />
</form>
<p>The Result is : <br>
<span id = "result"></span>
</p>
</body>
</html>
JAVASCRIPT CODE
function multiplyBy()
{
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
document.getElementById("result").innerHTML = num1 * num2;
}

function divideBy()
{
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
document.getElementById("result").innerHTML = num1 / num2;
}
OUTPUT
Convert temperatures
To and from celsius, Fahrenheit in
JAVASCRIPT
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Write a JavaScript program to convert temperatures to and from celsius,
fahrenheit</title>
</head>
<body>
</body>
</html>

JAVASCRIPT CODE
function cToF(celsius)
{
var cTemp = celsius;
var cToFahr = cTemp * 9 / 5 + 32;
var message = cTemp+'\xB0C is ' + cToFahr + ' \xB0F.';
console.log(message);
}

function fToC(fahrenheit)
{
var fTemp = fahrenheit ;
var fToCel = (fTemp - 32) * 5 / 9;
var message = fTemp+'\xB0F is ' + fToCel + '\xB0C.';
console.log(message);
}
cToF(60);
fToC(45);
OUTPUT
60°C is 140 °F.
45°F is 7.222222222222222°C.
Find the largest
Of three given integers
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to find the largest of three given integers.</title>
</head>
<body>

</body>
</html>

JAVASCRIPT CODE
function max_of_three(x, y, z)
{
max_val = 0;
if (x > y)
{
max_val = x;
} else
{
max_val = y;
}
if (z > max_val)
{
max_val = z;
}
return max_val;
}

console.log(max_of_three(1,0,1));
console.log(max_of_three(0,-10,-20));
console.log(max_of_three(1000,510,440));

Output:
1
0
1000
Reverse a given string
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to reverse a given string.</title>
</head>
<body>

</body>
</html>

JAVASCRIPT CODE
function string_reverse(str)
{
return str.split("").reverse().join("");
}

console.log(string_reverse("jsresource"));
console.log(string_reverse("www"));
console.log(string_reverse("JavaScript"));
OUTPUT
ecruosersj
www
tpircSavaJ
Replace every character
with the character following it in the alphabet
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to replace every character in a given string with the
character following it in the alphabet.</title>
</head>
<body>

</body>
</html>

JAVASCRIPT CODE
function string_reverse(str)
function LetterChanges(text) {
//https://goo.gl/R8gn7u
var s = text.split('');
for (var i = 0; i < s.length; i++) {
// Caesar cipher
switch(s[i]) {
case ' ':
break;
case 'z':
s[i] = 'a';
break;
case 'Z':
s[i] = 'A';
break;
default:
s[i] = String.fromCharCode(1 + s[i].charCodeAt(0));
}
// Upper-case vowels
switch(s[i]) {
case 'a': case 'e': case 'i': case 'o': case 'u':
s[i] = s[i].toUpperCase();
}
}
return s.join('');
}
console.log(LetterChanges("PYTHON"));
console.log(LetterChanges("W3R"));
console.log(LetterChanges("php"));

Output:
QZUIPO
X4S
qIq
Capitalize The First Letter
Of Each Word
Of A Given String
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to capitalize the first letter of each word of a given
string.</title>
</head>
<body>

</body>
</html>

JAVASCRIPT CODE
function capital_letter(str)
{
str = str.split(" ");

for (var i = 0, x = str.length; i < x; i++) {


str[i] = str[i][0].toUpperCase() + str[i].substr(1);
}

return str.join(" ");


}

console.log(capital_letter("Write a JavaScript program to capitalize the first


letter of each word of a given string."));

OUTPUT
Output:
Write A JavaScript Program To Capitalize The First Letter Of Each Word Of A
Given String.
Convert number to hours and
minutes
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to convert a given number to hours and minutes.
</title>
</head>
<body>

</body>
</html>

JAVASCRIPT CODE
function time_convert(num)
{
var hours = Math.floor(num / 60);
var minutes = num % 60;
return hours + ":" + minutes;
}

console.log(time_convert(71));
console.log(time_convert(450));
console.log(time_convert(1441));

OUTPUT
1:11
7:30
24:1
Count vowels in a given string
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to count the number of vowels in a given string.
</title>
</head>
<body>

</body>
</html>

JAVASCRIPT CODE
function vowel_Count(str)
{

return str.replace(/[^aeiou]/g, "").length;


}

console.log(vowel_Count("Python"));
console.log(vowel_Count("htmlresource.com"));

Output:
1
5
Create a new string
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to create a new string without the first and last
character.</title>
</head>
<body>

</body>
</html>
JAVASCRIPT CODE
function without_first_end(str) {
return str.substring(1, str.length - 1);
}
console.log(without_first_end('JavaScript'));
console.log(without_first_end('JS'));
console.log(without_first_end('PHP'));
Output:
avaScrip

H
Concatenate two strings
Except their first character
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to concatenate two strings except their first
character.</title>
</head>
<body>

</body>
</html>
JAVASCRIPT CODE
function concatenate(str1, str2) {
str1 = str1.substring(1, str1.length);
str2 = str2.substring(1, str2.length);
return str1 + str2;
}

console.log(concatenate("PHP","JS"));
console.log(concatenate("A","B"));
console.log(concatenate("AA","BB"));
Output:
HPS

AB
Move last three character
start of a specified string
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to move last three character to the start of a given
string. The string length must be 3.</title>
</head>
<body>

</body>
</html>

JAVASCRIPT CODE
function right_three(str) {
if (str.length > 1)
{
return str.slice(-3) + str.slice(0, -3);
}
return str;
}
console.log(right_three("Python"));
console.log(right_three("JavaScript"));
console.log(right_three("Hi"));

Output:
honPyt
iptJavaScr
Hi
Compute the sum
Of three elements of a given array
Of integers of length 3
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to compute the sum of three elements of an given
integers of length 3.</title>
</head>
<body>

</body>
</html>

JAVASCRIPT CODE
function sum_three(nums)
{
return nums[0] + nums[1] + nums[2];
}

console.log(sum_three([10, 32, 20]));


console.log(sum_three([5, 7, 9]));
console.log(sum_three([0, 8, -11]));

Output:
62
21
-3
Add two digits
Of a given positive integer
Of length two
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to add two digits of a given positive integer of
length two.</title>
</head>
<body>

</body>
</html>

JAVASCRIPT CODE
function add_two_digits(n)
{
return n % 10 + Math.floor(n / 10);
}
console.log(add_two_digits(25))
console.log(add_two_digits(50))

Output:
7
5
Check whether a given year is a leap
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Find Leap Year</title>
</head>
<body>
</body>
</html>

JAVASCRIPT CODE
function leapyear(year)
{
return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);
}
console.log(leapyear(2016));
console.log(leapyear(2000));
console.log(leapyear(1700));
console.log(leapyear(1800));
console.log(leapyear(100));

OUTPUT
true
true
false
false
false
Check given positive number
Multiple of 3 or a multiple of 7
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to check whether a given positive number is a
multiple of 3 or a multiple of 7. </title>
</head>
<body>

</body>
</html>

JAVASCRIPT CODE
function test37(x)
{
if (x % 3 == 0 || x % 7 == 0)
{
return true;
}
else {
return false;
}
}

console.log(test37(12));
console.log(test37(14));
console.log(test37(10));
console.log(test37(11));

Output:
true
true
false
false
Check a string starts with 'Java'
And false otherwise
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to check whether a string starts with 'Java' and false
otherwise.</title>
</head>
<body>
</body>
</html>

JAVASCRIPT CODE
function start_spec_str(str)
{
if (str.length < 4)
{
return false;
}
front = str.substring(0, 4);
if (front == 'Java')
{
return true;
}
else
{
return false;
}
}

console.log(start_spec_str("JavaScript"));
console.log(start_spec_str("Java"));
console.log(start_spec_str("Python"));

Output:
true
true
false
Check two given integer values
In the range 50..99
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to check whether two given integer values are in the
range 50..99 (inclusive). Return true if either of them are in the said range.
</title>
</head>
<body>

</body>
</html>

JAVASCRIPT CODE
function check_numbers(x, y)
{
if ((x >= 50 && x <= 99) || (y >= 50 && y <= 99))
{
return true;
}
els e
{
return false;
}
}

console.log(check_numbers(12, 101));
console.log(check_numbers(52, 80));
console.log(check_numbers(15, 99));
OUTPUT
false
true
true
Find a value which is nearest to 100
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to find a value which is nearest to 100 from two
different given integer values.</title>
</head>
<body>

</body>
</html>

JAVASCRIPT CODE
function near_100(x, y) {
if (x != y)
{
x1 = Math.abs(x - 100);
y1 = Math.abs(y - 100);

if (x1 < y1)


{
return x;
}
if (y1 < x1)
{
return y;
}
return 0;
}
else
return false;
}

console.log(near_100(90, 89));
console.log(near_100(-90, -89));
console.log(near_100(90, 90));

Output:
90
-89
false

You might also like