Posts

Showing posts from November, 2024

Generating Random Numbers

Image
  let num = Math . random (); console . log ( num ); num = num * 10 ; console . log ( num ); num = Math . floor ( num ); console . log ( num ); num = num + 1 ; console . log ( num ); let random = Math . floor ( Math . random () * 10 ) + 1 ; console . log ( random );

Math Functions

Image
  console . log ( Math . E ); console . log ( Math . PI ); // MATH.ABS(n) console . log ( Math . abs ( 5 )); console . log ( Math . abs ( - 5 )); console . log ( Math . abs ( - 15 )); // MATH.POW(a,b) console . log ( Math . pow ( 2 , 4 )); console . log ( Math . pow ( 3 , 3 )); // MATH.FLOOR(n) console . log ( Math . floor ( 5.5 )); console . log ( Math . floor ( 5.999999999 )); console . log ( Math . floor ( - 5.5 )); // MATH.CEIL(n) console . log ( Math . ceil ( 5.5 )); console . log ( Math . ceil ( 5.000011 )); console . log ( Math . ceil ( - 5.5 )); // MATH.RANDOM() console . log ( Math . random ()); console . log ( Math . random ()); console . log ( Math . random ());

Array Of Objects

Image
  const GOT = [     {         name : "Danerys" ,         role : "Queen" ,         aka : "Mother of Dragons" ,         dynasty : "Targareyn"     },     {         name : "Jon Snow" ,         role : "King" ,         aka : "The King In the North" ,     dynasty : "Stark/Targareyn"     } ]; console . log ( GOT ); // ACCESSING console . log ( GOT [ 0 ]. name ); console . log ( GOT [ 1 ]. name ); console . log ( GOT [ 0 ]. aka ); // UPDATING GOT [ 1 ]. name = "Aegon" ; console . log ( GOT [ 1 ]. name )

Object of Objects

Image
  let mahishmathi = {     baahu : {         role : "prince" ,         wife : "devasena"     },     balla : {         role : "Prince" ,         wife : "Unknown"     },     kattappa : {         role : "Banisa" ,         wife : null     } }; console . log ( mahishmathi ); // ACCESSING console . log ( mahishmathi . baahu . role ); console . log ( mahishmathi . kattappa . role ); // CHANGING mahishmathi . baahu . role = "king" ; console . log ( mahishmathi );

Adding Or Updating an Object

Image
  let mahishmathi = {     name : "baahubali" ,     age : 33 ,     height : "6'2ft" ,     weight : 100 ,     role : "prince" }; console . log ( mahishmathi ); // TO CHANGE mahishmathi . role = "king" ; mahishmathi . height = 6.2 ; // TO ADD mahishmathi . wife = "Devasena" ; console . log ( mahishmathi );

object keys to strings

Image
 dot operator(.) doesnot consider 1 as a string let obj = {   1 : "a" ,   2 : "b" ,   undefined : "c" ,   true : "d" ,   null : "e" , }; console . log ( obj [ "1" ]); console . log ( obj [ "2" ]); console . log ( obj [ "true" ]); console . log ( obj [ "null" ]); console . log ( obj [ "undefined" ]); console . log ( obj . true ); console . log ( obj . undefined ); console . log ( obj . null );

Get values

Image
  ACCESSING ELEMENTS IN OBJECTS: 1). DOT OPERATOR POST.LIKES 2)POST["LIKES"] let post = {     username : "@abhi71" ,     content : "first post" ,     likes : 151 ,     reposts : 3 ,     tags : [ "@elonmusk" , "@billgates" ] }; console . log ( post . username ); console . log ( post [ "username" ]); console . log ( post . likes ); console . log ( post [ "likes" ]);     console . log ( post [ "tags" ][ 0 ]);     console . log ( post . tags [ 0 ])     let prop = "likes" ;     console . log ( post . prop );     console . log ( post [ prop ]); LET PROP="LIKES" HERE PROP IS A VARIABLE VARIABLS CANNOT BE ACCESSED USING DOT OPERATOR

Creating a post(Twitter)

Image
  let post = {     username : "@abhi71" ,     content : "first post" ,     likes : 151 ,     reposts : 3 ,     tags : [ "@elonmusk" , "@billgates" ] };

Object Literals

Image
  let student = {   name : "abhii" ,   age : 20 ,   marks : 98 ,   city : "kmm" }; let hyd = {   lattitude : "29.714degN" ,   longitude : "71.7174degE" }; let arr = [ "abhii" , 20 , 98 , "kmm" ];

Assignment

Image
  // Q1 let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 2 , 3 ]; let num = 2 ; console . log ( ` ${ arr } ` ); for ( let i = 0 ; i < arr . length ; i ++ ){     if ( arr [ i ] == num ){         arr . splice ( i , 1 );     } } console . log ( ` ${ arr } ` ); / Q2 let number = 287152 ; let count = 0 ; let copy = number ; while ( copy > 0 ) {   count ++ ;   copy = Math . floor ( copy / 10 ); } console . log ( count ); // Q3 let sum = 0 ; let copy2 = number ; let rem = 0 ; while ( copy2 > 0 ) {   rem = copy2 % 10 ;   sum += rem ;   copy2 = Math . floor ( copy2 / 10 ); } console . log ( sum ); // Q4 let num2 = prompt ( "Enter number:" ); console . log ( "Number:" + num2 ); let fact = 1 ; for ( let i = 1 ; i < num2 ; i ++ ){ fact *= i ; } console . log ( "Factorial:" + fact ); // Q5 let arr2 = [ 10 , 6 , 9 , 69 , 121 ]; let largest = 0 ; for ( let i = 0 ; i < arr2 . length ; ...

TODO App

Image
 HTML <! DOCTYPE html > < html lang = "en" > < head >     < meta charset = "UTF-8" >     < meta name = "viewport" content = "width=device-width, initial-scale=1.0" >     < title > TO DO app </ title > </ head > < body >     < h1 > To Do app </ h1 >     < h2 > list-To print all tasks </ h2 >     < h2 > add-To add a task </ h2 >     < h2 > delete-To delelte a task </ h2 >     < h2 > quit-To quit from the game </ h2 > < script src = "TODOapp.js" ></ script > </ body > </ html > JS let todo = []; let req = prompt ( "Enter Request:" ); while ( true ) {   if ( req == "quit" ) {     console . log ( "Quitting Game..." );     break ;   }   else if ( req == "list" ) {     console . log ( "----------" );   ...

FOR OF Nested Arrays

Image
  let heroes = [[ "spiderman" , "ironman" , "thor" ],[ "batman" , "superman" , "flash" ]]; for ( list of heroes ){     console . log ( list );     for ( hero of list ){         console . log ( hero );     } }

FOR OF loop

Image
  let fruits = [ "mango" , "orange" , "grapes" , "banana" ]; for ( fruit of fruits ){     console . log ( fruit ); } let str = "abhiii" ; for ( char of str ){     console . log ( char ); }

Nested Loops With Nested Arrays

Image
  let heroes = [   [ "spiderman" , "ironman" , "thor" ],   [ "batman" , "superman" , "flash" ], ]; for ( let i = 0 ; i < heroes . length ; i ++ ) {   console . log ( i , heroes [ i ]);   for ( let j = 0 ; j < heroes [ 0 ]. length ; j ++ ) {     console . log ( j , heroes [ i ][ j ]);   } }

Loops With Arrays

Image
  let fruits = [ "oranges" , "mangoes" , "banana" , "grapes" , "apples" ]; for ( let i = 0 ; i < fruits . length ; i ++ ){     console . log ( i , fruits [ i ]); } // BACKWARDS for ( let j = fruits . length ; j >= 0 ; j -- ){     console . log ( j , fruits [ j ]); }

Guess the favourite Movie

Image
  const FavMovie = "salaar" ; let guess = prompt ( "Guess My Favourite Movie:" ); while (( guess != FavMovie ) && ( guess != "quit" )){     guess = prompt ( "Wrong guess.Try Again!!" ); } if ( guess == FavMovie ){     console . log ( "Correct Guess!!" ); } else {     console . log ( "U Quit.." ); }

WHILE Loop

Image
  let i = 1 ; while ( i <= 5 ){     console . log ( i );     i ++ ; } let j = 5 ; while ( j >= 1 ){     console . log ( j );     j -- ; }

Nested FOR loops

Image
  for ( let i = 1 ; i <= 4 ; i ++ ){     for ( let j = 1 ; j <= 3 ; j ++ ){         console . log ( i * j );     } }

Multiplication Table

Image
  let n = prompt ( "Ener n:" ); for ( let i = 1 ; i <= 10 ; i ++ ){     console . log ( n * i ); }

EVEN Numbers

Image
  for ( let i = 2 ; i <= 10 ; i ++ ){     if ( i % 2 == 0 ){         console . log ( i );     } } //  BACKWARDS for ( let i = 10 ; i >= 2 ; i -- ){     if ( i % 2 == 0 ){         console . log ( i );     } }

ODD Numbers

Image
  for ( let i = 0 ; i <= 15 ; i ++ ){     if ( i % 2 != 0 ){         console . log ( i );     } }

FOR Loop

Image
  for ( let i = 0 ; i <= 5 ; i ++ ){ console . log ( i ); }