parent
22d4c9b774
commit
b9d41468fe
@ -0,0 +1,36 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Document</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
|
||||||
|
<script>
|
||||||
|
let numChar = parseInt(prompt('Enter the number of charecters for id:',0));
|
||||||
|
let numId = parseInt(prompt('Enter the number of id :',0));
|
||||||
|
console.log(numChar,numId);
|
||||||
|
console.log( userIdGeneratedByUser(numChar,numId));
|
||||||
|
function userIdGeneratedByUser(Char,Id){
|
||||||
|
let id_s = [];
|
||||||
|
for(let i=0;i<Id;i++){
|
||||||
|
id_s.push(userIdGenerator(Char))
|
||||||
|
}
|
||||||
|
return id_s;
|
||||||
|
}
|
||||||
|
function userIdGenerator(Char) {
|
||||||
|
let idcode = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||||
|
let id = '';
|
||||||
|
for (let i = 0; i < numChar; i++) {
|
||||||
|
let rand = Math.floor(Math.random() * idcode.length);
|
||||||
|
id += idcode[rand];
|
||||||
|
}
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -1,27 +1,158 @@
|
|||||||
// 1.question
|
// 1.question
|
||||||
|
|
||||||
let personAccount = {
|
let personAccount = {
|
||||||
firstName: '',
|
firstName: "satya",
|
||||||
lastName: '',
|
lastName: "surendra",
|
||||||
incomes: 0,
|
incomes: 0,
|
||||||
expenses: 0,
|
expenses: 0,
|
||||||
|
totalIncome: function () {
|
||||||
totalIncome: function () {
|
return;
|
||||||
return;
|
},
|
||||||
},
|
totalExpenses: function () {
|
||||||
totalExpenses: function () {
|
return;
|
||||||
return;
|
},
|
||||||
},
|
accountInfo: function () {
|
||||||
accountInfo: function () {
|
return `Name : ${this.firstName} ${this.lastName}\nIncome: ${this.incomes}\nExpense: ${this.expenses}`;
|
||||||
return `Name : ${this.firstName} ${this.lastName}
|
},
|
||||||
Income: ${this.incomes} Expense: ${this.expenses}`
|
addIncome: function () {
|
||||||
},
|
return;
|
||||||
addIncome: function () {
|
},
|
||||||
return;
|
addExpense: function () {
|
||||||
},
|
return;
|
||||||
addExpense: function () {
|
},
|
||||||
return;
|
accountBalance: function () {
|
||||||
},
|
return;
|
||||||
accountBalance: function () {
|
},
|
||||||
return;
|
};
|
||||||
}
|
|
||||||
|
console.log(personAccount.accountInfo());
|
||||||
|
|
||||||
|
let users = [
|
||||||
|
{
|
||||||
|
_id: "ab12ex",
|
||||||
|
username: "Alex",
|
||||||
|
email: "alex@alex.com",
|
||||||
|
password: "123123",
|
||||||
|
createdAt: "08/01/2020 9:00 AM",
|
||||||
|
isLoggedIn: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
_id: "fg12cy",
|
||||||
|
username: "Asab",
|
||||||
|
email: "asab@asab.com",
|
||||||
|
password: "123456",
|
||||||
|
createdAt: "08/01/2020 9:30 AM",
|
||||||
|
isLoggedIn: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
_id: "zwf8md",
|
||||||
|
username: "Brook",
|
||||||
|
email: "brook@brook.com",
|
||||||
|
password: "123111",
|
||||||
|
createdAt: "08/01/2020 9:45 AM",
|
||||||
|
isLoggedIn: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
_id: "eefamr",
|
||||||
|
username: "Martha",
|
||||||
|
email: "martha@martha.com",
|
||||||
|
password: "123222",
|
||||||
|
createdAt: "08/01/2020 9:50 AM",
|
||||||
|
isLoggedIn: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
_id: "ghderc",
|
||||||
|
username: "Thomas",
|
||||||
|
email: "thomas@thomas.com",
|
||||||
|
password: "123333",
|
||||||
|
createdAt: "08/01/2020 10:00 AM",
|
||||||
|
isLoggedIn: false,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const products = [
|
||||||
|
{
|
||||||
|
_id: "eedfcf",
|
||||||
|
name: "mobile phone",
|
||||||
|
description: "Huawei Honor",
|
||||||
|
price: 200,
|
||||||
|
ratings: [
|
||||||
|
{ userId: "fg12cy", rate: 5 },
|
||||||
|
{ userId: "zwf8md", rate: 4.5 },
|
||||||
|
],
|
||||||
|
likes: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
_id: "aegfal",
|
||||||
|
name: "Laptop",
|
||||||
|
description: "MacPro: System Darwin",
|
||||||
|
price: 2500,
|
||||||
|
ratings: [],
|
||||||
|
likes: ["fg12cy"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
_id: "hedfcg",
|
||||||
|
name: "TV",
|
||||||
|
description: "Smart TV:Procaster",
|
||||||
|
price: 400,
|
||||||
|
ratings: [{ userId: "fg12cy", rate: 5 }],
|
||||||
|
likes: ["fg12cy"],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
// 2.question
|
||||||
|
|
||||||
|
let signUp = (name) => {
|
||||||
|
let len = users.filter((user) => user.username === name).length;
|
||||||
|
if (len) {
|
||||||
|
console.log(`This user already exits `);
|
||||||
|
} else {
|
||||||
|
users.push({
|
||||||
|
_id: name,
|
||||||
|
username: name,
|
||||||
|
email: `${name}@${name}.com`,
|
||||||
|
password: `${name}`,
|
||||||
|
createdAt: new Date(),
|
||||||
|
isLoggedIn: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 2.b.question
|
||||||
|
|
||||||
|
let signIn = (name, password) => {
|
||||||
|
let user = users.filter(
|
||||||
|
(user) => user.username === name && user.password === password
|
||||||
|
);
|
||||||
|
if (user.length) {
|
||||||
|
user.isLoggedIn = true;
|
||||||
|
console.log(`Login successful`);
|
||||||
|
} else {
|
||||||
|
console.log(`Invalid username or password`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 3.a.question
|
||||||
|
|
||||||
|
let rateProduct = (productId, userId, rate) => {
|
||||||
|
let product = products.find((p) => {
|
||||||
|
return p._id === productId;
|
||||||
|
});
|
||||||
|
product.ratings.push({
|
||||||
|
userId: userId,
|
||||||
|
rate: rate,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// 3.b.question
|
||||||
|
|
||||||
|
let avgRateing = (productId) => {
|
||||||
|
let product = products.find((p) => {
|
||||||
|
return p._id === productId;
|
||||||
|
});
|
||||||
|
let totalRate = 0;
|
||||||
|
product.ratings.forEach((p) => {
|
||||||
|
totalRate = p.rate + totalRate;
|
||||||
|
});
|
||||||
|
return totalRate / product.ratings.length;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in new issue