diff --git a/advanced/arrow-function.js b/advanced/arrow-function.js new file mode 100644 index 0000000..010d94a --- /dev/null +++ b/advanced/arrow-function.js @@ -0,0 +1,26 @@ +const square = num => num * num + +console.log(square(5)) + +const people = [{ + name: 'Arun', + age: 32 +}, { + name: 'Adwiti', + age: 5 +}, { + name: 'Arav', + age: 1 +}, { + name: 'Pushpa', + age: 31 +}] + +const under30 = people.filter(person => person.age < 30) +console.log(under30) + +//find a person with age 5 +//print the name of the person + +const equal5 = people.find(person => person.age ===5) +console.log(equal5) \ No newline at end of file diff --git a/advanced/arrow-function2.js b/advanced/arrow-function2.js new file mode 100644 index 0000000..d7ec6b2 --- /dev/null +++ b/advanced/arrow-function2.js @@ -0,0 +1,5 @@ +const add = function (a, b) { + return arguments[0] + arguments[1] + arguments[2] +} + +console.log(add(10,20,30)) \ No newline at end of file diff --git a/advanced/conditional.js b/advanced/conditional.js new file mode 100644 index 0000000..b74d76e --- /dev/null +++ b/advanced/conditional.js @@ -0,0 +1,35 @@ +let myAge = 27 +let message + + +if (myAge >= 18) { + message = 'you can vote' +} else { + message = 'you cannot vote' +} +console.log(message) + +//ternary operator +myAge = 5 + +message = myAge >= 18 ? 'You can vote' : 'You cannot vote' + +console.log(message) + + +const showPage = () => { + return 'Showing the page' +} + +const showErrorPage = () => { + return 'Showing the error page' +} + +const newMessage = myAge >= 10 ? showPage() : showErrorPage() +console.log(newMessage) + + +const team = ['Tyler', 'Porter'] + +const teamSize = team.length < 4 ? team.length : 'Too many people in the team' +console.log(teamSize) \ No newline at end of file diff --git a/advanced/grade-calc.js b/advanced/grade-calc.js new file mode 100644 index 0000000..8081464 --- /dev/null +++ b/advanced/grade-calc.js @@ -0,0 +1,34 @@ +const gradleCalc = (score, totalScore) => { + + if (typeof score !== 'number' || typeof totalScore !== 'number') { + throw Error('Please provide numbers') + } + const percentage = score / totalScore * 100 + + let grade = '' + + if (percentage > 90) { + grade = 'A' + } else if (percentage >= 80) { + grade = 'B' + } else if (percentage >= 70) { + grade = 'C' + } else if (percentage >= 60) { + grade = 'D' + } else if (percentage >= 50) { + grade = 'E' + } else { + grade = 'F' + } + + return `your got a grade of ${grade} with a percentage as ${percentage}` +} + +try { + const result = gradleCalc(true, 1000) + console.log(result) +} catch (e) { + console.log(e.message) +} + + diff --git a/advanced/truty-falsy.js b/advanced/truty-falsy.js new file mode 100644 index 0000000..22f747a --- /dev/null +++ b/advanced/truty-falsy.js @@ -0,0 +1,55 @@ +const products = [{ name: 'computer' }] +const product = products[0] + +//Truthy value which resolves to true +//Falsy - value the resolves to false +//Falsy - false, 0, empty string, null , undefined, NaN + + +//1. This will give true value, though the product is string +if (product) { + console.log("Product found") +} else { + console.log("Product not found") +} + +console.log('-------false--------') +if (false) { + console.log('this block will not be printed') +} else { + console.log('else block will be printed') +} +console.log('-------0--------') +if (0) { + console.log('this block will not be printed') +} else { + console.log('else block will be printed') +} + +console.log('--------Empty String-------') +if ('') { + console.log('this block will not be printed') +} else { + console.log('else block will be printed') +} + +console.log('--------null-------') +if (null) { + console.log('this block will not be printed') +} else { + console.log('else block will be printed') +} + +console.log('--------undefined-------') +if (null) { + console.log('this block will not be printed') +} else { + console.log('else block will be printed') +} + +console.log('--------NaN-------') +if (NaN) { + console.log('this block will not be printed') +} else { + console.log('else block will be printed') +} diff --git a/advanced/try-catch.js b/advanced/try-catch.js new file mode 100644 index 0000000..d538dc2 --- /dev/null +++ b/advanced/try-catch.js @@ -0,0 +1,18 @@ +const getTip = amount => { + + if (typeof amount === 'number') { + return amount * .25 + } else { + throw Error('Argument must be greater') + } + +} + +try { + console.log(getTip(10)) +} catch (e) { + console.log("Argument must be a number") +} + + + diff --git a/advanced/type-coercion.js b/advanced/type-coercion.js new file mode 100644 index 0000000..2245475 --- /dev/null +++ b/advanced/type-coercion.js @@ -0,0 +1,16 @@ +//Type coercion a String , a number , a boolean + +console.log('5' + 5) //o/p 55 +console.log('5' - 5) //o/p 0 +console.log(5 === 5) //true +console.log(5 == 5) //true +console.log('5' == 5) //true +console.log('5' === 5) //false + + +const type = typeof 123 +console.log(type) //number + +const value = true + 12 +console.log(typeof value) //number +console.log(value) //13 diff --git a/notes/fn.js b/notes/fn.js index 709f9f3..dfcce50 100644 --- a/notes/fn.js +++ b/notes/fn.js @@ -1,21 +1,24 @@ +'use strict' + const divElement = document.querySelector('#addNotesDiv') -const saveNotes = function (notes) { +const saveNotes = (notes) => { localStorage.setItem('notes', JSON.stringify(notes)) } -const getNotes = function () { +const getNotes = () => { const notes = localStorage.getItem('notes') - if (notes !== null) { - return JSON.parse(notes) - } else { + try{ + return notes != null ? JSON.parse(notes) : [] + }catch(e){ return [] } + } -//sort notes by one of the three ways -const sortNotes = function (notes, sortBy) { +//sort notes by one of the three ways, +const sortNotes = (notes, sortBy) => { if (sortBy === 'byEdited') { return notes.sort((a, b) => { @@ -50,7 +53,7 @@ const sortNotes = function (notes, sortBy) { }) } } -const renderNotes = function (notes, filters) { +const renderNotes = (notes, filters) => { if (filters !== undefined) { notes = sortNotes(notes, filters.sortBy) notes = notes.filter(note => { @@ -74,7 +77,7 @@ const renderNotes = function (notes, filters) { * if you want to add a particular item in a paragraph * @param {a specific note which you want to display} note */ -const generateDom = function (note) { +const generateDom = (note) => { const para = document.createElement('p') para.textContent = note.text return para @@ -84,7 +87,7 @@ const generateDom = function (note) { * if you want to add a button(x) with text next to it * @param {a specific note with a button before it} note */ -const generateDomWithXButton = function (note) { +const generateDomWithXButton = (note) => { const innerDivElement = document.createElement('div') const innerButtonElement = document.createElement('button') @@ -101,7 +104,7 @@ const generateDomWithXButton = function (note) { * * @param {add an event listener to the button} note */ -const generateDomWithXButtonAndWithEventListener = function (note) { +const generateDomWithXButtonAndWithEventListener = (note) => { //create a div element const innerDivElement = document.createElement('div') @@ -126,7 +129,7 @@ const generateDomWithXButtonAndWithEventListener = function (note) { * * @param {add an event listener to button and link to next page} note */ -const generateDomWithXElementAndLinkToNextPage = function (note) { +const generateDomWithXElementAndLinkToNextPage = (note) => { //create a div element const divEl = document.createElement('div') @@ -153,7 +156,7 @@ const generateDomWithXElementAndLinkToNextPage = function (note) { * * @param {deletes a note} id */ -const deleteNote = function (id) { +const deleteNote = (id) => { const index = notes.findIndex(note => note.id === id) notes.splice(index, 1) saveNotes(notes) @@ -164,6 +167,6 @@ const deleteNote = function (id) { * * @param {get the time from the timestamp passed} timestamp */ -const latestModifiedTime = function (timestamp) { +const latestModifiedTime = (timestamp) => { return `last modified ${moment(timestamp).fromNow()}` } diff --git a/oop/hangman.js b/oop/hangman.js new file mode 100644 index 0000000..2f695a2 --- /dev/null +++ b/oop/hangman.js @@ -0,0 +1,12 @@ +const Hangman = function(word, numberOfGuess){ + this.word = word + this.numberOfTries = numberOfGuess +} + +const first = new Hangman('cat',2) +const second = new Hangman('Peter',3) +console.log(first) +console.log(second) + + + diff --git a/oop/person.js b/oop/person.js new file mode 100644 index 0000000..1193323 --- /dev/null +++ b/oop/person.js @@ -0,0 +1,27 @@ +const Person = function (firstName, lastName, age, likes = []) { + this.firstName = firstName + this.lastName = lastName + this.age = age + this.likes = likes +} + +Person.prototype.getBio = function () { + let bio = `${this.firstName} is ${this.age} years old.` + this.likes.forEach(like => { + bio += ` ${this.firstName} likes ${like}` + }) + return bio +} + +Person.prototype.setName = function (fullName) { + const names = fullName.split(' ') + this.firstName = names[0] + this.lastName = names[1] +} + +const me = new Person('Arun', 'Singh', 10, ['Reading','Playing']) +me.setName('Alex Singh') +console.log(me.getBio()) + +const wife = new Person('Pushpa', 'Yadav', 5) +console.log(wife.getBio()) \ No newline at end of file