Posts

Showing posts from July, 2024

Practice Set on Arrays in JavaScript

  //Practice Set on Arrays //Question 1 : Create an array of numbers and take input from the user to add numbers to this array: let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]; let a = prompt ( "Enter a Number : " ); a = Number . parseInt ( a ); arr . push ( a ); console . log ( arr ); //Question 2 : Keep adding numbers to the array in question 1 until 0 is added to the array. let arr2 = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 83 ]; let a2 ; do {     a2 = prompt ( "Enter a Number : " );     a2 = Number . parseInt ( a2 );     arr2 . push ( a2 ); } while ( a2 != 0 ); console . log ( arr2 ); //Question 3 : Filter for numbers divisible by 10 from a given array. let arr3 = [ 1 , 2 , 30 , 4 , 50 , 6 , 7 , 83 , 670 ]; let n = arr3 . filter (( value ) => {     return value % 10 == 0 ; }); console . log ( n ); //Question 4 : Create an array of square of given numbers. let arr4 = [ 1 , 2 , 30 , 4 , 50 , 6 , 7 , 83 , 670 ]; let n4 ...

Map, Filter and Reduce in JavaScript

  //map() : Creates a new array by performing some operation on each array element. let arr = [ 45 , 23 , 21 ]; // Array map method let a = arr . map (( value , index , array ) => {     console . log ( value , index , array );     return value + index ; }); console . log ( a ); //filter() : Filters an array with values that passes a test. Creates a new array. //Array filter method let arr2 = [ 45 , 23 , 21 , 0 , 3 , 5 ]; let a2 = arr2 . filter (( value ) => {     return value < 10 ; }); console . log ( a2 ); //reduce() : Reduces an array to a single value //Array reduce method let arr3 = [ 1 , 2 , 3 , 5 , 2 , 1 ]; let newarr3 = arr3 . reduce (( h1 , h2 ) => {     return h1 + h2 ; }); console . log ( newarr3 ); OUTPUT: 45 0 [ 45, 23, 21 ] 23 1 [ 45, 23, 21 ] 21 2 [ 45, 23, 21 ] [ 45, 24, 23 ] [ 0, 3, 5 ] 14

Loops with Arrays in JavaScript

  let num = [ 3 , 5 , 1 , 2 , 4 ]; for ( let i = 0 ; i <= num . length ; i ++ ) {     console . log ( num [ i ]); } //ForEach Loop in JavaScript num . forEach (( element ) => {     console . log ( element * element ); }); //Array.from let name = "Harry" ; let arr = Array . from ( name ); console . log ( arr ); //for...of for ( let i of num ) {     console . log ( i ); } //for...in for ( let i in num ) {     console . log ( num [ i ]); } OUTPUT: 3 5 1 2 4 undefined 9 25 1 4 16 [ 'H', 'a', 'r', 'r', 'y' ] 3 5 1 2 4 3 5 1 2 4

Some more Array Methods in JavaScript

  let num = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]; let num_more = [ 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 ]; let num_even_more = [ 211 , 212 , 213 , 214 , 415 , 416 , 417 , 418 , 419 ]; console . log ( num . length ); delete num [ 0 ];   //"delete" is a Operator(just like "typeof" operator), Not method console . log ( num . length ); let newArray = num . concat ( num_more , num_even_more ); console . log ( newArray ); let numTwo = [ 551 , 22 , 3 , 14 , 5 , 6 , 7 , 8 , 229 ]; numTwo . sort ();   //sort() method is used to sort an array alphabetically console . log ( numTwo ); // sorting in Ascending Order let compare = ( a , b ) => {     return a - b ; } numTwo . sort ( compare ); console . log ( numTwo ); // sorting in Descending Order let compareTwo = ( a , b ) => {     return b - a ; } numTwo . sort ( compareTwo ); console . log ( numTwo ); let numThree = [ 551 , 22 , 3 , 14 , 5 , 6 , 7 , 8 , 229 ]; console...

Array Methods in JavaScript

  //Array Methods let num = [ 1 , 2 , 3 , 34 , 4 ]; let b = num . toString (); console . log ( b , typeof b ); let c = num . join ( "_" ); console . log ( c , typeof c ); let d = num . join ( " and " ); console . log ( d , typeof d ); let r = num . pop ();   //pop returns the popped element console . log ( num , r ); let r2 = num . push ( 56 );   //push returns the new array length console . log ( num , r2 ); let r3 = num . shift ();   //shift() Removes first element and returns it console . log ( r3 , num );   //Removes an element from the start of the array let r4 = num . unshift ( 78 ); //unshift() Adds element to the beginning.Returns new Array length console . log ( r4 , num ); OUTPUT : 1,2,3,34,4 string 1_2_3_34_4 string 1 and 2 and 3 and 34 and 4 string [ 1, 2, 3, 34 ] 4 [ 1, 2, 3, 34, 56 ] 5 1 [ 2, 3, 34, 56 ] 5 [ 78, 2, 3, 34, 56 ]

Introductions to Arrays in JavaScript

  //Arrays are variables which can hold more than one value let marks_class_12 = [ 91 , 62 , 33 , 84 , false , "Not Present" ]; console . log ( marks_class_12 ); console . log ( marks_class_12 [ 0 ]); console . log ( marks_class_12 [ 1 ]); console . log ( marks_class_12 [ 2 ]); console . log ( marks_class_12 [ 3 ]); console . log ( marks_class_12 [ 4 ]); console . log ( marks_class_12 [ 5 ]); console . log ( marks_class_12 [ 6 ]); //Will be undefined because index 6 does not exist console . log ( "The Length of marks_class_12 is :" , marks_class_12 . length ); marks_class_12 [ 6 ] = 98 ; //Adding a new value to an Array marks_class_12 [ 0 ] = 100 ; // Changing the value of an Array console . log ( marks_class_12 ); console . log ( "The Length of marks_class_12 is :" , marks_class_12 . length ); //In JavaScript, arrays are objects. The typeof operator on arrays returns object console . log ( typeof marks_class_12 ); //Arrays can hold many values under...

Practice Set on Strings in JavaScript

  //Chapter - 4 : Practice Set //Question 1 : What will the following print in JavaScript ? //console.log("har\"".length); let str = "har \" " ; console . log ( str . length ); //Question 2 : Explore the included, startsWith & endsWith functions of a string const sentence = "The quick brown for jumps over the lazy dog." ; const word = "fox2" ; console . log ( sentence . includes ( word ));   //.includes() always return True or False console . log ( `The word ' ${ word } ' ${ sentence . includes ( word ) ? 'is' : 'is not' } in the sentence` ); console . log ( sentence . startsWith ( "The" )); console . log ( sentence . startsWith ( "Sat" )); console . log ( sentence . startsWith ( "The" , 0 )); console . log ( sentence . startsWith ( "The" , 1 )); console . log ( sentence . endsWith ( "dog." )); console . log ( sentence . endsWith ( "Sat" )...

String Methods in JavaScript

  //Strings are immutable let name = "Harry" ; console . log ( name . length ); console . log ( name . toUpperCase ()); console . log ( name . toLowerCase ()); console . log ( name . slice ( 2 , 4 )); console . log ( name . slice ( 2 )); console . log ( name . replace ( "Har" , "Per" )); let friend = "Naman" ; console . log ( name . concat ( " is a friend of " , friend , " Ok" )); let friend2 = "         Meena           " ; console . log ( friend2 ); console . log ( friend2 . trim ()); let fr = "Shivika" ; console . log ( fr [ 0 ]); console . log ( fr [ 1 ]); console . log ( fr [ 2 ]); console . log ( fr [ 3 ]); console . log ( fr [ 4 ]); console . log ( fr [ 5 ]); console . log ( fr [ 6 ]); let fr2 = "Harry" + "Shivika" ; console . log ( fr2 ); let nameTwo = "Harry \" " ; console . log ( nameTwo . length ); let fr3 = "Naman" ; for ( let ...

Strings Introduction in JavaScript

  let name = "Harry"   //Here, double quotes is used console . log ( name . length ); console . log ( name [ 0 ]); console . log ( name [ 1 ]); console . log ( name [ 2 ]); console . log ( name [ 3 ]); console . log ( name [ 4 ]); let name2 = 'Harry' //Here, single quotes is used console . log ( name2 . length ); // Template literals let boy1 = "Pramod" ; let boy2 = "Nikhil" ; //Nikil is a friend of Pramod let sentence = ` ${ boy2 } is a friend of ${ boy1 } ` ; console . log ( sentence ); //Escape Sequence Characters let fruit = 'Bana \' na' ; console . log ( fruit ); let animal = "Elep \" hant" ; console . log ( animal ); let games = "super \n Mario" ;     //Newline console . log ( games ); let animalTwo = "Buff \t alo" ;     //Tab console . log ( animalTwo ); let fruitTwo = "Pine \r apple" ;   //Carriage return console . log ( fruitTwo ); OUTPUT : 5 H a r r ...

Practice set on Loops and Functions in JavaScript

//Practice set of Loops and Functions //Question 1 : Write a program to print the marks of a student in an object using for loop let marks = {     harry : 90 ,     shubham : 9 ,     lovish : 56 ,     Monika : 4 } for ( let i = 0 ; i < Object . keys ( marks ). length ; i ++ ) {     console . log ( "The marks of " + Object . keys ( marks )[ i ] + " are " + marks [ Object . keys ( marks )[ i ]]); } //Question 2 : Write a program in Question 1 using for in loop for ( let key in marks ) {     console . log ( "The marks of " + key + " are " + marks [ key ]); } //Question 3 : Write a program to print "try again" until the user enters the correct number let cn = 43 ; let i ; while ( i != cn ) {     console . log ( "Try again" );     i = prompt ( "Enter a number : " ); } console . log ( "You have entered a correct number" ); //Question 4 : Write a function to find mean of 5 nu...

Functions in JavaScript

  function onePlusAvg ( x , y ) {     return 1 + ( x + y ) / 2 ; } const sum = ( p , q ) => {     return p + q ; } const hello = () => {     console . log ( "Hey How are you ? I am fine" ); } const Morning = () => {     return "Hello, good morning" ; } let a = 1 ; let b = 2 ; let c = 3 ; hello (); console . log ( Morning ()); console . log ( "One plus Average of a and b is : " , onePlusAvg ( a , b )); console . log ( "One plus Average of b and c is : " , onePlusAvg ( b , c )); console . log ( "One plus Average of a and c is : " , onePlusAvg ( a , c )); console . log ( sum ( 9 , 7 )); OUTPUT : Hey How are you ? I am fine Hello, good morning One plus Average of a and b is : 2.5 One plus Average of b and c is : 3.5 One plus Average of a and c is : 3 16

while Loops in JavaScript

  //while loop let n = prompt ( "Enter the value of a : " ); n = Number . parseInt ( n ); let i = 0 ; while ( i < n ) {     console . log ( i );     i ++ ; } //do while loop let n2 = prompt ( "Enter the value of b : " ); let i2 = 0 ; do {     console . log ( i2 );     i2 ++ ; } while ( i2 < n2 );

For Loops in JavaScript

  for ( let i = 0 ; i < 10 ; i ++ ) {     console . log ( i ); } //Program to add first n natural numbers let sum = 0 let n = prompt ( "Enter the value of n" ); n = Number . parseInt ( n ); for ( let i = 0 ; i <= n ; i ++ ) {     sum = sum + i ; } console . log ( "Sum of first " + n + " natual numbers is " + sum ); let obj = {     harry : 90 ,     shubh : 45 ,     shivika : 56 ,     ritika : 57 ,     shiv : 23 } //For in loop for ( let a in obj ) {     console . log ( "Marks of " + a + " are " + obj [ a ]); } //For of loop for ( let b of "Harry" ) {     console . log ( b ); }