Assignment React Prescient Technologies Vivek Dhande
Assignment React Prescient Technologies Vivek Dhande
Simple calculator which will add two input number and display result on page
Code :
//Index.html
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Sandbox</title>
<meta charset="UTF-8" />
</head>
<body>
<div class="calculator" style="display: inline-grid">
<div>
<label for="">Number 1 </label>
<input type="number" id="num1" placeholder="Number 1" />
</div>
<div>
<label for="">Number 2 </label>
<input type="number" id="num2" placeholder="Number 2" />
</div>
<div><button id="addButton">+</button></div>
<div>
<label for="">Result </label>
<input type="number" id="result" disabled />
</div>
<div id="error"></div>
</div>
<script src="./index.mjs" type="module"></script>
</body>
</html>
// index.js
import "./styles.css";
const addButton = document.getElementById("addButton");
const num1Input = document.getElementById("num1");
const num2Input = document.getElementById("num2");
addButton.addEventListener("click", function () {
try {
const num1 = parseFloat(document.getElementById("num1").value);
const num2 = parseFloat(document.getElementById("num2").value);
if (isNaN(num1) || isNaN(num2)) {
throw new Error("Invalid input. Please enter valid numbers.");
}
const result = num1 + num2;
const resultDisplay = document.getElementById("result");
resultDisplay.value =
Number.isInteger(num1) && Number.isInteger(num2)
? result
: result.toFixed(2);
} catch (error) {
const resultDisplay = document.getElementById("error");
resultDisplay.textContent = `Error: ${error.message}`;
}
});
function resetErr(params) {
const resultDisplay = document.getElementById("error");
resultDisplay.textContent = ``;
}
num1Input.addEventListener("keyup", resetErr);
num2Input.addEventListener("keyup", resetErr);
2. Create HTML page to Accept First Name and Last Name of user. When the input field
gets focus then change its background color and when focus goes out it should change
to previous color.
Code :
// Index.html
<!DOCTYPE html>
<html>
<head>
<title>Program 2</title>
<meta charset="UTF-8" />
</head>
<body>
<div class="parentDiv" style="display: inline-grid">
<div>
<label for="">Enter your firstname </label>
<input type="text" id="firstName" class="input-field" />
</div>
<div>
<label for="">Enter your lastname </label>
<input type="tex" id="lastName" class="input-field" />
</div>
</div>
<script src="./index.mjs" type="module"></script>
</body>
</html>
//Index.js
import "./styles.css";
document.querySelectorAll(".input-field").forEach((input) => {
input.addEventListener("focus", (event) => {
event.target.style.backgroundColor = "#pink";
});
input.addEventListener("blur", (event) => {
event.target.style.backgroundColor = "";
});
});
3. Create HTML page to display any image on webpage with some specific weight and
height. When user hover mouse over it then it should scale that image with 1.5X factor.
Code :
// Index.html
<!DOCTYPE html>
<html>
<head>
<title>Program 3</title>
<meta charset="UTF-8" />
</head>
<body>
<div class="image-container">
<img
src="https://53.fs1.hubspotusercontent-
na1.net/hub/53/hubfs/image8-2.jpg?width=893&height=600&name=image8-
2.jpg"
alt="Sample Image"
/>
</div>
<script src="./index.mjs" type="module"></script>
</body>
</html>
//Style.css
body {
font-family: sans-serif;
}
.image-container {
width: 300px; /* specify desired width */
height: 200px; /* specify desired height */
overflow: hidden; /* hide any overflow */
}
.image-container img {
width: 100%;
height: 100%;
transition: transform 0.3s ease;
}
.image-container img:hover {
transform: scale(1.5);
}
4. Write simple async function which takes two number as input and return addition of
it in promise. This function should validate input argument for number if not then it
should throw error.
Call this function
• With valid number and display result (not promise) on console.
• With invalid argument and display error messages
Code
handleAddition(1, 2);