Skip to content

Commit

Permalink
es6
Browse files Browse the repository at this point in the history
  • Loading branch information
nagabhushanamn committed Mar 10, 2018
1 parent 8b7cf84 commit 964ef38
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 11 deletions.
50 changes: 39 additions & 11 deletions Todo-app/app.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@


//--------------------------------------------------------
// Model ( e.g Todo )

//--------------------------------------------------------
// Model ( e.g Todo )
class Todo {
constructor(id, title) {
this.id = id;
this._title = title;
this.completed = false
}
set title(title) {
this._title = title;
}
get title() {
return this._title;
}
}
//-------------------------------------------------------
// Service ( e.g TodoService)





class TodoService {
constructor() {
this.todos = [];
}
addTodo(title) {
TodoService.todoId++;
let newTodo = new Todo(TodoService.todoId, title);
this.todos.push(newTodo);
}
deleteTodo(title) {
this.todos.forEach( (item)=> {
if (item.title === title) {
let idx = this.todos.indexOf(item);
this.todos.splice(idx, 1);
}
});
}
}
TodoService.todoId = 0;
//--------------------------------------------------------
// Test it on console
let service = new TodoService();
service.addTodo('eat lunch');
service.addTodo('sleep');
service.addTodo('go home');
//----------------------------------------------------------


//--------------------------------------------------------
164 changes: 164 additions & 0 deletions Todo-app/array-methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@



// let products = [
// { id: 21313, name: 'Product1', price: 3000 },
// { id: 34234, name: 'Product2', price: 1000 },
// { id: 12312, name: 'Product3', price: 4000 },
// { id: 64564, name: 'Product4', price: 3000 }
// ]

//-------------------------------------------------------


//let newItem={ id: 46757, name: 'Product5', price: 8000 }
// products.push(newItem);

//let newProducts=products.concat(newItem); // immutable

// let extraProducts=[
// { id: 46757, name: 'Product5', price: 8000 },
// { id: 34234, name: 'Product6', price: 6000 }
// ];

// let result=products.concat(extraProducts);
// let result=[...products,...extraProducts];


//-------------------------------------------------------

// every() & some()

// let products = [
// { id: 21313, name: 'Product1', price: 3000 },
// { id: 34234, name: 'Product2', price: 1000 },
// { id: 12312, name: 'Product3', price: 4000 },
// { id: 64564, name: 'Product4', price: 3000 }
// ]

// // let result=products.every(function (item) {
// // return item.price >= 1000;
// // });
// let result=products.some(function (item) {
// return item.price > 8000;
// });
// console.log(result);

//-------------------------------------------------------

// filter()


// let products = [
// { id: 21313, name: 'Product1', price: 3000 },
// { id: 34234, name: 'Product2', price: 1000 },
// { id: 12312, name: 'Product3', price: 4000 },
// { id: 64564, name: 'Product4', price: 3000 }
// ]

// let result=products.filter((item,idx)=>item.price>1000);

//-------------------------------------------------------

// let products = [
// { id: 21313, name: 'Product1', price: 3000 },
// { id: 34234, name: 'Product2', price: 1000 },
// { id: 12312, name: 'Product3', price: 4000 },
// { id: 64564, name: 'Product4', price: 3000 }
// ]

// let result=products.map((item,idx)=>{
// return {
// name:item.name,
// price:item.price
// }
// });

// let nums=[1,2,3,4,5,6,7,8,9];
// let sqNums=nums.map(n=>n*n);

//-------------------------------------------------------

// reduce()

// let products = [
// { id: 21313, name: 'Product1', price: 3000 },
// { id: 34234, name: 'Product2', price: 1000 },
// { id: 12312, name: 'Product3', price: 4000 },
// { id: 64564, name: 'Product4', price: 3000 }
// ]

// let totalPrice=products.reduce((sum,item)=>{
// console.log(sum);
// console.log(item);
// return sum+item.price;
// },0);
// console.log(totalPrice);

//-------------------------------------------------------

// slice()

// let products = [
// { id: 21313, name: 'Product1', price: 3000 },
// { id: 34234, name: 'Product2', price: 1000 },
// { id: 12312, name: 'Product3', price: 4000 },
// { id: 64564, name: 'Product4', price: 3000 }
// ]

// let result=products.slice(0,2);

//-------------------------------------------------------

// splice()

// let products = [
// { id: 21313, name: 'Product1', price: 3000 },
// { id: 34234, name: 'Product2', price: 1000 },
// { id: 12312, name: 'Product3', price: 4000 },
// { id: 64564, name: 'Product4', price: 3000 }
// ]

// products.splice(0,2,{ id: 23424, name: 'Product5', price: 45345 },{ id: 24234, name: 'Product6', price: 45345 });


//-------------------------------------------------------

// forEach

// let products = [
// { id: 21313, name: 'Product1', price: 3000 },
// { id: 34234, name: 'Product2', price: 1000 },
// { id: 12312, name: 'Product3', price: 4000 },
// { id: 64564, name: 'Product4', price: 3000 }
// ]
// products.forEach((item,idx)=>{
// console.log(item);
// });

//-------------------------------------------------------


// let item = { id: 12312, name: 'Product3', price: 4000 };

// let products = [
// { id: 21313, name: 'Product1', price: 3000 },
// { id: 34234, name: 'Product2', price: 1000 },
// item,
// { id: 64564, name: 'Product4', price: 3000 },
// item
// ]
// console.log(Array.isArray(products));
// products.pop();


// let menu=["biryani-1","biryani-3","biryani-3","biryani-4"];
// let result=menu.join(" ");

// let idx = products.indexOf(item);


//-------------------------------------------------------

let item="1234567890";
let result=Array.from(item);
14 changes: 14 additions & 0 deletions Todo-app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Todo - app </h1>
<hr/>
<script src="./array-methods.js"></script>
</body>
</html>

0 comments on commit 964ef38

Please sign in to comment.