week4.1 DOM
// eg: adding two number by taking user input. **
<!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>
<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 populateDiv() {
const a = document.getElementById("firstNumber").value;
const b = document.getElementById("secondNumber").value;
const element = document.getElementById("finalSum")
element.innerHTML = parseInt(a) + parseInt(b);
}
</script>
<input id="firstNumber" type="text" placeholder=" First number"></input>
<br>
<input id="secondNumber" type="text" placeholder=" Second number"></input>
<br>
<button onclick="populateDiv()">Calculate</button>
<br>
<div id="finalSum"></div>
</body>
</html>
<script>
function populateDiv() {
const a = document.getElementById("firstNumber").value;
const b = document.getElementById("secondNumber").value;
const element = document.getElementById("finalSum")
element.innerHTML = parseInt(a) + parseInt(b);
}
</script>
<input id="firstNumber" type="text" placeholder=" First number"></input>
<br>
<input id="secondNumber" type="text" placeholder=" Second number"></input>
<br>
<button onclick="populateDiv()">Calculate</button>
<br>
<div id="finalSum"></div>
</body>
</html>
//Calculating the sum in real time**
<!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>
async function populateDiv() {
const a = document.getElementById("firstNumber").value;
const b = document.getElementById("secondNumber").value;
const element = document.getElementById("finalSum")
element.innerHTML = parseInt(a) + parseInt(b);
}
</script>
<input onchange="populateDiv()" id="firstNumber" type="text" placeholder=" First number"></input>
<br>
<input onchange="populateDiv()" id="secondNumber" type="text" placeholder=" Second number"></input>
<br>
<div id="finalSum"></div>
</body>
</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>
async function populateDiv() {
const a = document.getElementById("firstNumber").value;
const b = document.getElementById("secondNumber").value;
const element = document.getElementById("finalSum")
element.innerHTML = parseInt(a) + parseInt(b);
}
</script>
<input onchange="populateDiv()" id="firstNumber" type="text" placeholder=" First number"></input>
<br>
<input onchange="populateDiv()" id="secondNumber" type="text" placeholder=" Second number"></input>
<br>
<div id="finalSum"></div>
</body>
</html>
//Here we created a backend that will help to calculate the sum of two numbers #ugly way**
<!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 populateDiv() {
const a = document.getElementById("firstNumber").value;
const b = document.getElementById("secondNumber").value;
const element = document.getElementById("finalSum")
fetch("https://sum-server.100xdevs.com/sum?a=0" + a + "&b" + b)
.then(function (response) {
response.text()
.then(function (ans) {
console.log(ans);
document.getElementById("finalSum").innerHTML = ans;
})
});
// element.innerHTML = parseInt(a) + parseInt(b);
}
</script>
<input id="firstNumber" type="text" placeholder=" First number"></input><br>
<input id="secondNumber" type="text" placeholder=" Second number"></input><br>
<button onclick="populateDiv()">Click to calculate</button>
< <div id="finalSum"></div>
</body>
</html>
//Here we created a backend that will help to calculate the sum of two numbers # in a pretty way**
// frontend that will take user input
<!-- //here we created a backend that will help to calculate the sum of two numbers -->
<!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>
<input id="firstNumber" type="text" placeholder=" First number"></input><br>
<input id="secondNumber" type="text" placeholder=" Second number"></input><br>
<button onclick="populateDiv()">Click to calculate</button>
< <div id="finalSum"></div>
<script>
async function populateDiv2() {
const a = document.getElementById("firstNumber").value;
const b = document.getElementById("secondNumber").value;
const response = await fetch("https://sum-server.100xdevs.com/sum?a=0" + a + "&b" + b)
const ans = await response.text();
document.getElementById("finalSum").innerHTML = ans;
}
</script>
</body>
</html>
// Backend server that will calculate the sum of two digits**
const express = require("express");
const app = express();
app.get("/sum", (req, res) => {
const a = parseInt(req.query.a);
const b = parseInt(req.query.b);
const sum = a+b;
res.send(sum.toString());
});
app.get("/interest", (req,res) => {
const principal = parseInt(req.query.principal);
const rate = parseInt(req.query.rate);
const time = parseInt(req.query.time);
const interest = (principal*rate*time)/100;
const total = principal + interest;
res.send({
total: total,
interest: interest,
})
})
let http = require('http'),
fs = require('fs');
fs.readFile('./index.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(80);
});
//debouncing is a method that works when the user finishes, and then it will send the query in the backend/delead requests sent.**
<!-- //here we created a backend that will help to calculate the sum of two numbers -->
<!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>
<input oninput="debouncePopulateDiv()" id="firstNumber" type="text" placeholder=" First number"></input><br>
<input oninput="debouncePopulateDiv()" id="secondNumber" type="text" placeholder=" Second number"></input><br>
<button onclick="populateDiv()">Click to calculate</button>
< <div id="finalSum">
</div>
<script>
//delay the call to populateDiv until i have not been called for 100ms
// and I've been called at least once
//clear the timeOut
let timeout ;
function debouncePopulateDiv() {
clearTimeout(timeout)
setTimeout(() => {
populateDiv()
}, 100);
}
async function populateDiv() {
const a = document.getElementById("firstNumber").value;
const b = document.getElementById("secondNumber").value;
const response = await fetch("./index.js" + a + "&b" + b)
const ans = await response.text();
document.getElementById("finalSum").innerHTML = ans;
}
</script>
</body>
</html>
Comments