Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
aks-9 authored Jun 28, 2022
1 parent 7a807a3 commit 93592dd
Show file tree
Hide file tree
Showing 84 changed files with 2,565 additions and 0 deletions.
5 changes: 5 additions & 0 deletions 14_Operator precedence.js
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);
15 changes: 15 additions & 0 deletions 15_Swapping exercise.js
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);
9 changes: 9 additions & 0 deletions 16_If_Else_ statments.js
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');
15 changes: 15 additions & 0 deletions 17_Switch_case.js
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');

}
23 changes: 23 additions & 0 deletions 18_For_Loop.js
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);
}
11 changes: 11 additions & 0 deletions 19_While_Loop.js
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++;
}
18 changes: 18 additions & 0 deletions 20_Do_While_Loop.js
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 );
15 changes: 15 additions & 0 deletions 21_For_In_Loop.js
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]);
6 changes: 6 additions & 0 deletions 22_For_Of_Loop.js
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);
};
15 changes: 15 additions & 0 deletions 23_Break_Continue_Statements.js
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++;
}

35 changes: 35 additions & 0 deletions 24_Exercise_Functions_Max_number.js
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));
15 changes: 15 additions & 0 deletions 25_Exercise_Functions_Landscape_Portrate.js
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);
45 changes: 45 additions & 0 deletions 26_Exercise_If_Else_FizzBuzz.js
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));
47 changes: 47 additions & 0 deletions 27_Exercise_Nested_If_Else_Demerit_Points.js
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);
12 changes: 12 additions & 0 deletions 28_Exercise_For_Loop_Odd_Even.js
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);
17 changes: 17 additions & 0 deletions 29_Exercise_Array.js
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);



20 changes: 20 additions & 0 deletions 30_Exercise_Objects_String.js
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);
32 changes: 32 additions & 0 deletions 31_Exercise_Sum of Multiples 3 and 5.js
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);
Loading

0 comments on commit 93592dd

Please sign in to comment.