Skip to content

Commit 56dee6f

Browse files
committed
all labs
0 parents  commit 56dee6f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+3772
-0
lines changed

final/jim-app

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit e9dfd5732146ead052cc2e5c112921038c0af541

lab1/Rana_Aparajita copy.zip

1.85 KB
Binary file not shown.

lab1/Rana_Aparajita.zip

5.21 KB
Binary file not shown.

lab1/arrays.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
let myStringArray = ['hello', 'world', 'my', 'name', 'is', 'Patrick'];
2+
let myNumArray = [1, 2, 3, 4, 5];
3+
4+
let mixedArray = [
5+
1,
6+
'Hello',
7+
undefined,
8+
true,
9+
(message) => {
10+
console.log(message);
11+
}
12+
];
13+
14+
mixedArray[4]('Hello world!');
15+
16+
myStringArray.forEach((value) => {
17+
console.log(value);
18+
});
19+
20+
let myNumArraySquared = myNumArray.map((x) => {
21+
return x * x;
22+
});
23+
24+
console.log(myNumArray);
25+
console.log(myNumArraySquared);
26+
// console.log(myNumArray);
27+
28+
let oddNumbers = myNumArray.filter((num) => {
29+
return num % 2 === 1;
30+
});
31+
32+
// // // console.log(myNumArray);
33+
console.log(oddNumbers);
34+
35+
let sumOfOdds = oddNumbers.reduce((currentTotal, newValue) => {
36+
const newTotal = currentTotal + newValue;
37+
return newTotal;
38+
}, 0);
39+
40+
console.log(sumOfOdds);
41+
42+
// console.log(myNumArray);
43+
myNumArray.push(6);
44+
console.log(myNumArray);
45+
myNumArray.push('Patrick');
46+
console.log(myNumArray);
47+
console.log(myNumArray.pop());
48+
console.log(myNumArray);
49+
50+
// console.log(myNumArray.join('&&'));

lab1/hello.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let message = 'Hello, world';
2+
console.log(message);
3+
4+
console.log('Hello, World!');

lab1/lab1.js

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
//I pledge my honor that I have abided by the Stevens Honor System - Aparajita Rana
2+
3+
const { promises } = require("fs");
4+
5+
const questionOne = function questionOne(arr) {
6+
// Implement question 1 here
7+
8+
if(arr == undefined){
9+
return {};
10+
}
11+
if(arr.length==0){
12+
return {};
13+
}
14+
15+
let prime={};
16+
17+
for(let x = 0; x < arr.length; x++){
18+
let num = arr[x];
19+
let temp = true;
20+
//base cases
21+
if(num == 0 || num == 1){
22+
temp = false;
23+
}
24+
//check if divisable by anything -> if it is then not prime ret false
25+
else{
26+
for(let y = 2; y <= num/2; y++){
27+
if(num%y == 0){
28+
temp = false;
29+
}
30+
}
31+
}
32+
//set the res into the right place
33+
prime[num] = temp ? true : false;
34+
}
35+
return prime;
36+
}
37+
38+
const questionTwo = function questionTwo(arr) {
39+
// Implement question 2 here
40+
41+
if(arr.length===0 || arr==undefined){
42+
return 0;
43+
}
44+
45+
let myNumArraySquared = arr.map((x) => {
46+
return x * x;
47+
});
48+
49+
let sum = myNumArraySquared.reduce((a, b) => a + b, 0);
50+
let temp = Math.sqrt(Math.pow(sum, 5));
51+
52+
//toFixed returns string -> we want number
53+
return Number(temp.toFixed(2));
54+
55+
}
56+
57+
const questionThree = function questionThree(text) {
58+
// Implement question 3 here
59+
let vals= {consonants: 0, vowels: 0, numbers: 0, spaces: 0, punctuation: 0, specialCharacters: 0}
60+
if(text.length<=0 || text==undefined){
61+
return vals;
62+
}
63+
64+
// regex format: g -> string i -> ignores case
65+
var vow = text.match(/[aeiou]/gi);
66+
vals.vowels = (vow!=null) ? vow.length : 0;
67+
68+
var num = text.match(/[01233456789]/gi);
69+
vals.numbers = (num!=null) ? num.length : 0;
70+
71+
vals.spaces = (text.split(" ").length - 1);
72+
73+
var punc = text.match(/[.,':;!?]/gi);
74+
vals.punctuation = (punc!=null) ? punc.length : 0;
75+
76+
var spec = text.match(/[@#$%^&*]/gi);
77+
vals.specialCharacters = (spec!=null) ? spec.length : 0;
78+
79+
var cons = text.match(/[bcdfghjklmnpqrstvwxzy]/gi);
80+
vals.consonants = (cons!=null) ? cons.length : 0;
81+
82+
return vals;
83+
}
84+
85+
const questionFour = function questionFour(num1, num2, num3) {
86+
// Implement question 4 here
87+
if(num1<0 || num2<0 || num3<0){
88+
return -1;
89+
}
90+
91+
// Loan Payment (P) = Amount (A) / Discount Factor (D)
92+
var rate = (num2 / 100) /12;
93+
var mon = num3 * 12;
94+
var temp = Math.pow((1+rate), mon);
95+
96+
if (rate === 0) {
97+
var res = num1 / mon;
98+
return parseFloat(res.toFixed(2), 10);
99+
}
100+
else {
101+
var res2 = (num1 * rate * temp) / (temp - 1);
102+
return parseFloat(res2.toFixed(2), 10);
103+
}
104+
}
105+
106+
module.exports = {
107+
firstName: "Aparajita",
108+
lastName: "Rana",
109+
studentId: "10440384",
110+
questionOne,
111+
questionTwo,
112+
questionThree,
113+
questionFour
114+
};

lab1/lab1.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const lab1 = require("./lab1");
2+
3+
console.log(lab1.questionOne([24, 11, 33]));
4+
console.log(lab1.questionOne([45, 73, 100]));
5+
console.log(lab1.questionOne([1]));
6+
console.log(lab1.questionOne([85, 103, 97]));
7+
console.log(lab1.questionOne([52, 57, 99, 113]));
8+
console.log(lab1.questionOne([]));
9+
console.log(lab1.questionOne());
10+
11+
console.log(lab1.questionTwo([3,2,1]));
12+
console.log(lab1.questionTwo([10, 3, 5]));
13+
console.log(lab1.questionTwo([14,22,30]));
14+
console.log(lab1.questionTwo([25, 180, 93]));
15+
console.log(lab1.questionTwo([23, 37, 19]));
16+
console.log(lab1.questionTwo([]));
17+
18+
console.log(lab1.questionThree("Hello World."));
19+
console.log(lab1.questionThree("Web Programming seems pretty fun!"));
20+
console.log(lab1.questionThree("yikes!?"));
21+
console.log(lab1.questionThree("Once upon a time not long ago, I had a farm"));
22+
console.log(lab1.questionThree("Today is the Super Bowl. I don't watch football but I really like the Weeknd so I still watched"));
23+
console.log(lab1.questionThree(""));
24+
25+
console.log(lab1.questionFour(45000, 4, 8));
26+
console.log(lab1.questionFour(52000, 7, 11));
27+
console.log(lab1.questionFour(30500, 5.2, 8));
28+
console.log(lab1.questionFour(29520, 8.1, 3));
29+
console.log(lab1.questionFour(52210, 2, 5));
30+
console.log(lab1.questionFour(33030, 5.5, 1));

lab10/Rana_Aparajita_CS546_WS.zip

5.76 KB
Binary file not shown.

lab10/app.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//I pledge my honor that I have abided byt the Stevens Honor System - Aparajita Rana
2+
const session = require('express-session');
3+
const express = require("express");
4+
const app = express();
5+
const configRoutes = require("./routes");
6+
const exphbs = require("express-handlebars");
7+
app.use('/public', express.static(__dirname + '/public'));
8+
9+
app.use(express.json());
10+
app.use(express.urlencoded({ extended: true }));
11+
12+
app.use(session({
13+
name: 'AuthCookie',
14+
secret: 'supersecretstring',
15+
resave: false,
16+
saveUninitialized: true
17+
}));
18+
19+
app.engine('handlebars', exphbs({defaultLayout : "main"}));
20+
21+
app.set('view engine', 'handlebars');
22+
23+
//logging middleware
24+
app.use('*',(req, res, next)=>{
25+
console.log("[%s]: %s %s (%s)", new Date().toUTCString(), req.method, req.originalUrl, `${req.session.user ? "Authenticated User" : "Non-Authenticated User"}`);
26+
next();
27+
});
28+
29+
configRoutes(app);
30+
31+
app.listen(3000, () => {
32+
console.log("We've now got a server!");
33+
console.log('Your routes will be running on http://localhost:3000');
34+
});

lab10/data/users.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module.exports = [
2+
{
3+
_id: 0,
4+
username: "masterdetective123",
5+
hashedPassword: "$2a$16$7JKSiEmoP3GNDSalogqgPu0sUbwder7CAN/5wnvCWe6xCKAKwlTD.",
6+
firstName: "Sherlock",
7+
lastName: "Holmes",
8+
profession: "Detective",
9+
bio: "Sherlock Holmes (/ˈʃɜːrlɒk ˈhoʊmz/) is a fictional private detective created by British author " +
10+
"Sir Arthur Conan Doyle. Known as a \"consulting detective\" in the stories, Holmes is known for a" +
11+
" proficiency with observation, forensic science, and logical reasoning that borders on the fantastic," +
12+
" which he employs when investigating cases for a wide variety of clients, including Scotland Yard."
13+
14+
}, // etc, dont forget the other data
15+
{
16+
_id: 1,
17+
username: "lemon",
18+
hashedPassword: "$2a$16$SsR2TGPD24nfBpyRlBzINeGU61AH0Yo/CbgfOlU1ajpjnPuiQaiDm",
19+
firstName: "Elizabeth",
20+
lastName: "Lemon",
21+
profession: "Writer",
22+
bio: "Elizabeth Miervaldis \"Liz\" Lemon is the main character of the American television series 30 Rock. She " + "created and writes for the fictional comedy-sketch show The Girlie Show or TGS with Tracy Jordan."
23+
} // etc, dont forget the other data
24+
];

lab10/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "cs546-lab-10",
3+
"version": "1.0.0",
4+
"description": "authentication and middleware",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node app.js"
9+
},
10+
"author": "Aparajita Rana 10440384",
11+
"license": "ISC",
12+
"dependencies": {
13+
"bcrypt": "^3.0.6",
14+
"express": "^4.16.4",
15+
"express-handlebars": "^3.0.2",
16+
"express-session": "^1.16.1"
17+
}
18+
}

lab10/routes/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const pageRoutes = require('./pages');
2+
3+
const constructorMethod = (app) => {
4+
app.use('/', pageRoutes);
5+
6+
app.use('*', (req, res) => {
7+
res.sendStatus(404);
8+
});
9+
};
10+
11+
module.exports = constructorMethod;

lab10/routes/pages.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const bcrypt = require('bcrypt');
2+
const users = require('../data/users');
3+
const express = require("express");
4+
const router = express.Router();
5+
6+
//authentication middleware
7+
//reference: https://scotch.io/tutorials/route-middleware-to-check-if-a-user-is-authenticated-in-node-js
8+
function isAuthenticated(req, res, next) {
9+
if (req.session.user)
10+
return next();
11+
else res.status(403).render("invalid", {title:"Invalid Entry"});
12+
}
13+
14+
router.get('/', function(req, res) {
15+
if (req.session.user) {
16+
res.redirect('/private')
17+
} else {
18+
res.render('login', {title: "Login"})
19+
}
20+
});
21+
22+
router.post('/login', async(req, res) => {
23+
//to fix: "Cannot set headers after they are sent to the client"
24+
let inval = false;
25+
if(req.body.username && req.body.password){
26+
for(let user in users){
27+
if(users[user].username == req.body.username){
28+
let match = await bcrypt.compare(req.body.password, users[user].hashedPassword);
29+
30+
if(match){
31+
req.session.user = {
32+
'username': users[user].username,
33+
'password':users[user].hashedPassword,
34+
'_id':users[user]._id,
35+
'firstName':users[user].firstName,
36+
'lastName':users[user].lastName,
37+
'profession':users[user].profession,
38+
'bio':users[user].bio};
39+
40+
res.redirect('/private');
41+
}
42+
else{
43+
inval = true;
44+
}
45+
}
46+
else{
47+
inval = true;
48+
}
49+
}
50+
}
51+
else{
52+
inval=true;
53+
}
54+
if(inval){
55+
res.status(401).render('login', {title : "Incorrect Username and/or Password", error:"Incorrect Username and/or Password"});
56+
}
57+
/* catch(err){
58+
res.status(401);
59+
res.render('login', {message : "Incorrect Username and/or Password", error:"Incorrect Username and/or Password"});
60+
} */
61+
});
62+
63+
router.get("/private",isAuthenticated,(req,res) => {
64+
//console.log(req.session.user);
65+
res.render('details', req.session.user);
66+
});
67+
68+
router.get("/logout", async (req, res) => {
69+
//res.clearCookie("AuthCookie");
70+
req.session.destroy();
71+
res.render('logout', { message: "Logged out successfully!" });
72+
});
73+
74+
module.exports = router

lab10/views/details.handlebars

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<h1>User Information: </h1>
2+
<p>ID: {{_id}} </p>
3+
<p>Username: {{username}} </p>
4+
<p>First Name: {{firstName}}</p>
5+
<p>Last Name: {{lastName}}</p>
6+
<p>Profession: {{profession}}</p>
7+
<p>Bio: {{bio}}</p>
8+
<a href="/logout">Logout</a>

lab10/views/invalid.handlebars

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p>You did not provide a valid username and/or password</p>
2+
<a href="/">Login</a>

0 commit comments

Comments
 (0)