Professional Documents
Culture Documents
we-lab(ans)
we-lab(ans)
let a,b,rest;
[a,b,...rest]=[10,20,30,"hello","world"]
console.log(rest)
function sample(){
return ["This","is","sample","code"]
}
let [result]=sample()
console.log(result)
//objects
let person={name1:"john",age:25}
let {name1}=person
console.log(name1)
let {name1:id,age:age1}=person
console.log(id)
console.log(age1)
//Q-9 : Write a javascript code for combining yield values.
function *gen1(i){
yield i+1;
yield i+2;
}
function *gen2(i){
yield i;
yield * gen1(i)
yield i+4
}
const g=gen2(10)
console.log(g.next().value)
console.log(g.next().value)
console.log(g.next().value)
console.log(g.next().value)
function fetchData(id) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (id === 1) {
resolve(data);
}else {
reject('Invalid ID');
}
}, 2000);
});
}
//Generating 0 or 1 randomly
const v = Math.floor(Math.random() * 2);
fetchData(v)
.then(data => {
console.log('Data fetched successfully:', data);
})
.catch(error => {
console.error('Error fetching data:', error);
});
//Q-11 : Demonstrate async and await
function fetchData() {
return new Promise(resolve => {
setTimeout(() => {
resolve('Data fetched!');
}, 2000);
});
}
getData();
app.use(cookie());
app.put("/students/:id",async (req,res)=>{
const sid=parseInt(req.params.id)
const student=await stds.findOneAndUpdate({id:sid},req.body, { new: true })
if(!student)
return res.send(`student with id ${sid} doesn't exists`)
res.send(student)
})
app.delete("/students/:id",async(req,res)=>{
const sid=parseInt(req.params.id)
const student= await stds.findOneAndDelete({id:sid})
if(!student)
return res.send(`student with id ${sid} doesn't exists`)
res.send(`student ${student} is deleted`)
})
app.listen(2000, () => {
console.log("server is running on port 2000");
})
//Q-27 : Create an application to render dynamic content in web applications
(EJS) .
const express = require("express")
const app = express()
app.use(express.json())
app.set("view engine", "ejs");
let students = [
{ id: 1, name: "abc", branch: "ece" },
{ id: 2, name: "pqr", branch: "cse" },
{ id: 3, name: "xyz", branch: "it" }
]
app.get("/", (req, res) => {
res.send("hello world")
})
app.listen(2000, () => {
console.log("server is running on port 2000");
})
std.ejs
<!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>
<center>
<table border="1">
<tr style="border: 3px solid black;">
<th>ID</th>
<th>name</th>
<th>branch</th>
</tr>
<tr>
<td>
<%= student.id %>
</td>
<td>
<%= student.name %>
</td>
<td>
<%= student.branch%>
</td>
</tr>
</table>
</center>
</body>
</html>
students.ejs
<!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>
<center>
<h1>Items</h1>
<table border="1">
<tr style="border: 3px solid black;">
<th>ID</th>
<th>Name</th>
<th>Branch</th>
</tr>
<% students.forEach(std=> { %>
<tr>
<td>
<%= std.id %>
</td>
<td>
<%= std.name %>
</td>
<td>
<%= std.branch %>
</td>
</tr>
<% }); %>
</table>
</center>
</body>
</html>
//Q: write a program to read a file and write into a file using fs module
const fs=require('fs')
fs.readFile("input.txt",(err,data)=>{
if (err) {
console.error('Error reading file:', err);
return;
}
fs.writeFile("output.txt",data,'utf8',(err)=>{
if (err) {
console.error('Error writing to file:', err);
return;
}
console.log('File successfully written')
})
})
readableStream.setEncoding('utf8');
readableStream.on('data', function(chunk) {
writableStream.write(chunk);
console.log("successfully copied")
});
//Q:Implement and write the procedure to perform push and pull request
>mkdir dir
>cd dir
>git config --global user.name “<username>”
>git config --global user.email “<user email>”
>git init
>git branch b1
>git checkout b1
>touch file.txt
>git add .
>git commit -m “file created”
>git remote add origin <user github repository url>
>git push origin b1