Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions advanced/arrow-function.js
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 5 additions & 0 deletions advanced/arrow-function2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const add = function (a, b) {
return arguments[0] + arguments[1] + arguments[2]
}

console.log(add(10,20,30))
35 changes: 35 additions & 0 deletions advanced/conditional.js
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 34 additions & 0 deletions advanced/grade-calc.js
Original file line number Diff line number Diff line change
@@ -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)
}


55 changes: 55 additions & 0 deletions advanced/truty-falsy.js
Original file line number Diff line number Diff line change
@@ -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')
}
18 changes: 18 additions & 0 deletions advanced/try-catch.js
Original file line number Diff line number Diff line change
@@ -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")
}



16 changes: 16 additions & 0 deletions advanced/type-coercion.js
Original file line number Diff line number Diff line change
@@ -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
31 changes: 17 additions & 14 deletions notes/fn.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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 => {
Expand All @@ -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
Expand All @@ -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')
Expand All @@ -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')

Expand All @@ -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')

Expand All @@ -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)
Expand All @@ -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()}`
}
12 changes: 12 additions & 0 deletions oop/hangman.js
Original file line number Diff line number Diff line change
@@ -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)



27 changes: 27 additions & 0 deletions oop/person.js
Original file line number Diff line number Diff line change
@@ -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())