Week_13_Intangible_Technologies_Assessment_Intro_to_JavaScript(Alojipan) (1)
Week_13_Intangible_Technologies_Assessment_Intro_to_JavaScript(Alojipan) (1)
(INTANGIBLE TECHNOLOGIES)
EXERCISE
13
PROBLEM SOLVING USING JAVASCRIPT
Name Role
Members (if Group):
● Understand and apply core JavaScript concepts: Students will be able to define and use key
JavaScript concepts such as variables, data types, functions, loops, conditionals, and objects to
write simple programs and manipulate data effectively.
Prerequisites:
● Knowledge of JavaScript
● Notepad++ or VSCode
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reverse a Number</title>
</head>
<body>
<script>
function reverseNumber(x) {
// Convert the number to a string, reverse it, and convert it back to a number
var reversed = x.toString().split('').reverse().join('');
return parseInt(reversed);
}
// Test Data
var x = 32243;
document.write("Reversed number: " + reverseNumber(x));
</script>
</body>
</html>
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sort Letters Alphabetically</title>
</head>
<body>
<script>
function sortStringAlphabetically(str) {
// Convert string to an array of characters, sort the array, and join it back into a string
return str.split('').sort().join('');
}
// Test Data
var inputString = 'webmaster';
document.write("Sorted string: " + sortStringAlphabetically(inputString));
</script>
</body>
</html>
Number of Vowels
Write a JavaScript function that accepts a string as a parameter and counts the
number of vowels within the string.
Note : As the letter 'y' can be regarded as both a vowel and a consonant, we do
not count 'y' as vowel here.
Sample Data and output:
Example string : 'The quick brown fox'
Expected Output : 5
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Count Vowels</title>
</head>
<body>
<script>
function countVowels(str) {
// Define vowels
const vowels = 'aeiouAEIOU';
let count = 0;
return count;
}
// Test Data
var inputString = 'The quick brown fox';
document.write("Number of vowels: " + countVowels(inputString));
</script>
</body>
</html>
Write a JavaScript function that accepts a string as a parameter and converts the first letter of
each word of the string in upper case.
Example string : 'the quick brown fox'
Expected Output : 'The Quick Brown Fox '
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Capitalize First Letter</title>
</head>
<body>
<script>
function capitalizeFirstLetter(str) {
// Split the string into words
const words = str.split(' ');
// Test Data
var inputString = 'the quick brown fox';
document.write("Converted string: " + capitalizeFirstLetter(inputString));
</script>
</body>
</html>
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Get Current Date</title>
</head>
<body>
<script>
function getCurrentDate() {
// Get the current date
let currentDate = new Date();
// Get the individual components of the date
let day = currentDate.getDate();
let month = currentDate.getMonth() + 1; // Months are 0-indexed
let year = currentDate.getFullYear();
</body>
</html>
Write a JavaScript program to calculate number of days left until next Christmas.
Write a JavaScript program to calculate multiplication and division of two numbers (input from
user).
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator & Date Functions</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f4f4f9;
}
.container {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
text-align: center;
width: 300px;
}
input {
margin: 5px 0;
padding: 5px;
width: 80%;
}
button {
margin: 5px;
padding: 8px 12px;
}
</style>
</head>
<body>
<div class="container">
<h2>Get Current Date</h2>
<button onclick="getCurrentDate()">Show Current Date</button>
<div class="result" id="result"></div>
<h2>Calculator</h2>
<div>
<input type="number" id="num1" placeholder="1st Number">
<input type="number" id="num2" placeholder="2nd Number">
</div>
<button onclick="calculate('multiply')">Multiply</button>
<button onclick="calculate('divide')">Divide</button>
<div class="calc-result" id="calc-result"></div>
<script>
function getCurrentDate() {
let currentDate = new Date();
let day = currentDate.getDate();
let month = currentDate.getMonth() + 1;
let year = currentDate.getFullYear();
document.getElementById("result").innerHTML =
'Format 1: ' + format1 + '<br>' +
'Format 2: ' + format2 + '<br>' +
'Format 3: ' + format3 + '<br>' +
'Format 4: ' + format4;
}
function daysUntilChristmas() {
let today = new Date();
let currentYear = today.getFullYear();
let nextChristmas = new Date(currentYear, 11, 25);
function calculate(operation) {
let num1 = parseFloat(document.getElementById("num1").value);
let num2 = parseFloat(document.getElementById("num2").value);
let result = 0;
if (isNaN(num1) || isNaN(num2)) {
document.getElementById("calc-result").innerHTML = "Please enter valid numbers.";
return;
}
function findSign() {
let num1 = parseFloat(document.getElementById("signNum1").value);
let num2 = parseFloat(document.getElementById("signNum2").value);
let num3 = parseFloat(document.getElementById("signNum3").value);
if (isNaN(num1) || isNaN(num2) || isNaN(num3)) {
alert("Please enter all three numbers.");
return;
}
</body>
</html>
Output
function findSign() {
let num1 = parseFloat(document.getElementById("signNum1").value);
let num2 = parseFloat(document.getElementById("signNum2").value);
let num3 = parseFloat(document.getElementById("signNum3").value);
Write a simple JavaScript program to join all elements of the following array into a string.
Expected Output :
"Red,Green,White,Black"
"Red,Green,White,Black"
"Red+Green+White+Black"
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Join Array Elements</title>
</head>
<body>
<script>
// Define the array
var colors = ["Red", "Green", "White", "Black"];
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sort Array Items</title>
</head>
<body>
<script>
// Define the array
var arr1 = [3, 8, 7, 6, 5, -4, 3, 2, 1];
</body>
</html>
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Color Choices</title>
</head>
<body>
<script>
// Define the color array
var color = ["Blue ", "Green", "Red", "Orange", "Violet", "Indigo", "Yellow "];
</body>
</html>
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.
Sample Output :
"0 is even"
"1 is odd"
"2 is even"
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Odd or Even</title>
</head>
<body>
<script>
// Iterate from 0 to 2
for (var i = 0; i <= 2; i++) {
if (i % 2 === 0) {
// If the number is even
document.write(i + " is even<br>");
} else {
// If the number is odd
document.write(i + " is odd<br>");
}
}
</script>
</body>
</html>
Write a JavaScript conditional statement to find the largest of five numbers. Display an alert box
to show the result.
Sample numbers : -5, -2, -6, 0, -1
Output : 0
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Largest of Five Numbers</title>
</head>
<body>
<script>
// Define the five numbers
var num1 = -5, num2 = -2, num3 = -6, num4 = 0, num5 = -1;
Write a JavaScript conditional statement to sort three numbers. Display an alert box to show the
result.
Sample numbers : 3, -7, 2
Output : The sign is –
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sort and Check Sign of Three Numbers</title>
</head>
<body>
<script>
// Prompt the user for three numbers
var num1 = parseInt(prompt("Enter the first number:"));
var num2 = parseInt(prompt("Enter the second number:"));
var num3 = parseInt(prompt("Enter the third number:"));
// Display the sorted numbers and the sign of their product in an alert box
alert("Sorted numbers: " + numbers.join(", ") + "\nThe sign is: " + sign);
</script>
</body>
</html>
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Last Day of the Month</title>
</head>
<body>
<script>
// Function to get the last day of a month
function lastday(year, month) {
// Create a new date object for the first day of the next month
var date = new Date(year, month + 1, 0); // 0 day means the last day of the previous month
return date.getDate(); // Returns the last day of the month
}
// Test the function with the provided test data
document.write(lastday(2014, 0) + "<br>"); // January (0)
document.write(lastday(2014, 1) + "<br>"); // February (1)
document.write(lastday(2014, 11) + "<br>"); // December (11)
</script>
</body>
</html>
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculate Age</title>
</head>
<body>
<script>
// Function to calculate age based on date of birth
function calculate_age(dob) {
var today = new Date();
var age = today.getFullYear() - dob.getFullYear();
var m = today.getMonth() - dob.getMonth();
return age;
}
</body>
</html>
Write a JavaScript function to get the week end date
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Get Weekend Date</title>
</head>
<body>
<script>
function getWeekendDate(date) {
// Get the day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
var dayOfWeek = date.getDay();
// Calculate the days to add to get to the next Saturday (6) or Sunday (0)
var daysToSaturday = 6 - dayOfWeek; // days to next Saturday
var daysToSunday = (dayOfWeek === 0) ? 0 : 7 - dayOfWeek; // days to next Sunday
// Get the next Saturday or Sunday
var saturday = new Date(date);
saturday.setDate(date.getDate() + daysToSaturday);
// Test Data
var date = new Date(); // Current date
document.write(getWeekendDate(date));
</script>
</body>
Part 2. Question and Answer
Evaluate each of the following Javascript expressions and show the value.
Problem Value
Trace the following code by showing the values of the 3 variables in the table on the right, for each line of
code that is executed (after the line is executed):
Code X Y Z