-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
84 changed files
with
2,565 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
let x = 2 + 3 * 4 // Multiply has precedence over addition. | ||
console.log('x is', x); | ||
|
||
let y = ( 2 + 3 ) * 4 // Values within the brackets are executed first. | ||
console.log('y is', y); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//Swapping exercise | ||
|
||
let a = 'red'; | ||
let b = 'blue'; | ||
|
||
let x = a; // copying the value of a into an empty variable x. | ||
|
||
a = b; // overwriting the value of a, by copying the value of b into a. | ||
|
||
b = x; // overwriting the value of b, by copying the value of x into b. | ||
|
||
|
||
|
||
console.log(a); | ||
console.log(b); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let hour = 20; | ||
if ( hour >= 6 && hour < 12 ){ | ||
console.log("Good Morning"); | ||
} | ||
else if( hour >=12 && hour < 18){ | ||
console.log('Good afternoon'); | ||
} | ||
else | ||
console.log('Good evening'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
let role = 'Unknown'; | ||
|
||
switch(role){ | ||
case 'guest': | ||
console.log('Guest user'); | ||
break; | ||
|
||
case 'moderator': | ||
console.log('Moderator User'); | ||
break; | ||
|
||
default: | ||
console.log('Unknown User'); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//Print numbers 0 to 4 . | ||
for ( let i = 0; i < 5; i++){ | ||
console.log('Hello World', i); | ||
} | ||
|
||
//Print numbers 1 to 5. | ||
for ( let j = 1; j <= 5; j++){ | ||
console.log('Hello World', j); | ||
} | ||
|
||
|
||
|
||
//Display odd numbers b/w 1 to 5. | ||
console.log("Odd nos are:"); | ||
for (let n = 1; n <= 5; n++){ | ||
if ( n % 2 !== 0) console.log(n); | ||
} | ||
|
||
//Display odd numbers b/w 1 to 5 in reverse order. | ||
console.log("Odd nos in reverse order are:"); | ||
for( let m = 5; m >= 1; m--){ | ||
if ( m % 2 !== 0) console.log(m); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// For loop printing first 5 odd numbers. | ||
for (let i=0; i<=5; i++){ | ||
if (i%2 !== 0) console.log(i); | ||
} | ||
|
||
// While loop printing first 5 odd numbers. | ||
let i = 0; // external declaration of variable. | ||
while (i <= 5){ | ||
if ( i%2 !== 0) console.log(i); | ||
i++; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// For loop printing first 5 odd numbers. | ||
// for (let i=0; i<=5; i++){ | ||
// if (i%2 !== 0) console.log(i); | ||
// } | ||
|
||
// While loop printing first 5 odd numbers. | ||
// let i = 0; | ||
// while (i <= 5){ | ||
// if ( i%2 !== 0) console.log(i); | ||
// i++; | ||
// } | ||
|
||
//Do-While loop | ||
|
||
let j = 11; | ||
do{ if (j % 2 !== 0 ) console.log(j); | ||
j++; | ||
} while( j <=5 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//for-in loop | ||
|
||
//Declaring an object named Person | ||
const person = { | ||
name: 'ash', //key:value | ||
age: 30 | ||
}; | ||
|
||
for( let key in person) | ||
console.log(key, person[key]); | ||
|
||
|
||
const colors = ['red', 'green', 'blue']; | ||
for( let index in colors) | ||
console.log(index, colors[index]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//For-Of loop displays only elements of an array. | ||
|
||
const colors = ['red','green','blue']; | ||
for(let color of colors){ | ||
console.log(color); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//Break and continue statements for loops. | ||
|
||
let i=0; | ||
while(i<=10){ | ||
// if(i===5) break; | ||
|
||
if(i%2===0){ | ||
i++; | ||
continue; // execution goes back to the starting of the loop. | ||
} | ||
|
||
console.log(i); | ||
i++; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//Find max number | ||
|
||
// function max (n1, n2){ | ||
|
||
// if( n1 > n2) | ||
// console.log("Maximum no. is", n1); | ||
|
||
// else if( n2 > n1) | ||
// console.log("Maximum no. is", n2); | ||
|
||
// else | ||
// console.log('Both numbers are equal'); | ||
// }; | ||
|
||
// max(20,2); | ||
|
||
|
||
|
||
// function max1(n1,n2){ | ||
// if ( n1 > n2) return n1; | ||
// else // don't really need this. | ||
// return n2; | ||
|
||
// }; | ||
|
||
|
||
// console.log(max1(5,13)); | ||
|
||
|
||
|
||
function max2(a,b){ | ||
return a > b ? a : b; | ||
}; | ||
|
||
console.log(max2(16,6)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// function isLandscape(width, height){ | ||
// return width > height ? 'Yes' : 'No'; | ||
|
||
// }; | ||
|
||
// console.log(isLandscape(1600, 800)); | ||
|
||
|
||
|
||
function isLandscape(width, height){ | ||
return console.log((width > height)); // no need to pass yes or no, the expression returns true or false on its own. | ||
|
||
}; | ||
|
||
isLandscape(1200, 800); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//Divisible by 3 => Fizz | ||
//Divisible by 5 => Buzz | ||
//Divisible by both 3 and 5 => FizzBuzz | ||
//Not Divisible by 3 or 7 => same input | ||
//Not a number = 'Not a number' | ||
|
||
|
||
// function fizzBuzz(input){ | ||
// if ( typeof input !== 'number') // gives type of value | ||
// return NaN; //Not a number | ||
|
||
// else if (( input % 3 === 0 ) && (input % 5 === 0 )) | ||
// return 'FizzBuzz'; | ||
|
||
// else if ( input % 3 === 0 ) | ||
// return 'Fizz'; | ||
|
||
// else if (input % 5 === 0 ) | ||
// return 'Buzz'; | ||
|
||
// else | ||
// return input; | ||
|
||
// }; | ||
// console.log(fizzBuzz(15)); | ||
|
||
//We can also remove the else part to make it cleaner. | ||
|
||
function fizzBuzz(input){ | ||
if ( typeof input !== 'number') // gives type of value | ||
return NaN; //Not a number | ||
|
||
if (( input % 3 === 0 ) && (input % 5 === 0 )) | ||
return 'FizzBuzz'; | ||
|
||
if ( input % 3 === 0 ) | ||
return 'Fizz'; | ||
|
||
if (input % 5 === 0 ) | ||
return 'Buzz'; | ||
|
||
return input; //no need of if statment as this is the only option left. | ||
|
||
}; | ||
console.log(fizzBuzz(15)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
speed limit is 70 | ||
5 => 1 point | ||
Math.floor(1.3) | ||
12 points => suspended | ||
*/ | ||
|
||
|
||
function checkSpeed(speed){ | ||
const speedLimit = 70; | ||
const kmPerPoint = 5; | ||
|
||
if(speed < speedLimit + kmPerPoint) | ||
return console.log('Ok'); | ||
|
||
const points = Math.floor((speed - speedLimit)/ kmPerPoint); | ||
|
||
|
||
if (points > 12) | ||
console.log('License Suspended!'); | ||
else | ||
console.log('Point are:', points); | ||
} | ||
|
||
checkSpeed(135); | ||
|
||
|
||
|
||
// function checkSpeed(speed){ | ||
// const speedLimit = 70; | ||
// const kmPerPoint = 5; | ||
|
||
// if (speed < speedLimit + kmPerPoint){ | ||
// console.log('Ok'); | ||
// return; // this stops the execution here and code after this if block doesn't get executed. | ||
// // Helps us to remove the indentation. | ||
// } | ||
|
||
// const points = Math.floor((speed - speedLimit) / kmPerPoint ); | ||
|
||
// if (points > 12) | ||
// console.log('License suspended'); | ||
// else | ||
// console.log('Points are:', points); | ||
|
||
// } | ||
// checkSpeed(75); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function showNumbers(limit){ | ||
for(i=0; i<=limit;i++){ | ||
// if(i%2 === 0) console.log(i,"Even"); | ||
// else console.log(i,'Odd'); | ||
|
||
const message = (i%2 ===0) ? 'Even' : 'Odd'; | ||
console.log(i,message); | ||
}; | ||
|
||
}; | ||
|
||
showNumbers(10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function countTruthy(numbers){ | ||
counter=0; | ||
for( let index of numbers){ | ||
if(index) | ||
counter++; | ||
} | ||
return console.log(counter); | ||
} | ||
|
||
|
||
|
||
const numbers = [0,1,2,3,4,'five', 6, null, NaN,undefined,false] | ||
|
||
countTruthy(numbers); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const movie = { | ||
title: 'a', | ||
releaseYear: 2018, | ||
rating: 4.5, | ||
director: 'b' | ||
}; | ||
|
||
function showProperties(obj){ | ||
|
||
for( let key in obj){ | ||
|
||
if (typeof obj[key] === 'string') | ||
console.log(key,obj[key]); | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
showProperties(movie); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//multiple of 3: 3,6,9,12 | ||
//multiple of 5: 5,10 | ||
|
||
|
||
|
||
// function sum(limit){ | ||
// let sum = 0; | ||
|
||
// for(let i=0; i<= limit;i++) | ||
// if (i % 3 === 0 || i % 5 === 0) | ||
// sum += i; | ||
|
||
|
||
// return sum; | ||
|
||
// } | ||
|
||
// console.log(sum(12)); | ||
|
||
|
||
|
||
function sum(limit){ | ||
let total= 0; | ||
for(let i=0; i<= limit; i++){ | ||
if(i % 3 === 0 || i % 5 === 0) | ||
total += i; | ||
} | ||
return console.log(total); | ||
|
||
} | ||
|
||
sum(10); |
Oops, something went wrong.