Posts

Showing posts from November, 2021

JavaScript Challenge-10

 /*      Create a function that find sum of squares of every digit of a number.      Example: -         squareDigits(123) ➞ (1^2 + 2^2 + 3^2) = 14         squareDigits(24) ➞ (2^2+4^2) = 20         squareDigits(3212) ➞ (3^2+2^2+1^2+2^2) = 18 */ let num = 428; let digits = num.toString().split(' '); let realDigits = digits.map(Number); let result = 0; for(var i=0; i<realDigits.length; i++){     result = result + ((realDigits[i] * realDigits[i])); } console.log(result);

JavaScript Challenge-9

 /* Create a function that takes two arguments: the original price and the discount percentage as integers and returns the final price after the discount. */ let original_price; let discount; let result; function discountedPrice(price, disc){      if(disc<100){         result = ((price)*((100 - disc)))/100;      }else{         result = "Please Enter valid value";      }      console.log(result); } discountedPrice(2000, 25);

JavaScript Coding Challenge-8

 Write a program which removes 0 from prefix but not suffix..        e.g., - if, i/p - 300, (o/p - 300)        if, i/p - 003, (o/p - 3) _______________________________________  let original_num; let result; function removeZeros(number){       if(number%10 == 0){           result = original_num;       }else{           result = (number%10);       }       console.log(result); } original_num = 009; removeZeros(original_num);

JavaScript Challenge - 7

 Create a function that takes a country's name and its area as arguments and returns the area of the country's proportion of the total world's landmass. ------------------------------------------------------------------------------------------------------------   let world_land = 148940000;  // (in KM. square) function areaOfLand(country, area){      let country_area = ((area/world_land)*100).toFixed(2);      let result = "Area of "+country+" is "+country_area+" % of the total Land Mass";      console.log(result); } areaOfLand("India", 2973190);

Print the Inital Letters of First Name and Last Name

let name = "Rana Saha"; let str; let str1; let str2; str = name.split(" "); for(var i=0; i<name.length; i++){  str1 = str[0][0];  str2 = str[1][0]; } console.log(str1+str2);

Reverse String

 function reverse(str){    let stack = [];    // push letter into stack    for(var i=0; i<str.length; i++){         stack.push(str[i]);    }      // pop letter from the stack    let revers_string = '';    while(stack.length > 0){            revers_string += stack.pop();    }    return reverseStr; } console.log(reverse('JavaScript Stack')); //kcatS tpircSavaJ

Fibonacci sequence using recursion

 // program to display fibonacci sequence using recursion     function fibonacci(num) {         if(num < 2) {             return num;         }         else {             return fibonacci(num-1) + fibonacci(num - 2);         }     }     // take nth term input from the user     const nTerms = prompt('Enter the number of terms: ');     if(nTerms <=0) {         console.log('Enter a positive integer.');     }     else {         for(let i = 0; i < nTerms; i++) {             console.log(fibonacci(i));         }  ...

Factorial using Recursion

 // program to find the factorial of a number    function factorial(x){      // if number is 0    if(x == 0){        return 1;    }        // if number is positive    else{    return (x * factorial(x - 1));    }    }        const number = 4;        // calling factorial() if num is non-negative    if(number > 0){         let result= factorial(number);         alert(result);    }

Average of All Indexes of Array

 let array_1 = [1, 7, 9, 12, 6, 10, 2, 11, 9, 8];     let average;     let total = 0;     for(var i=0; i<array_1.length; i++){           total = ((total + array_1[i]));     }              average = (total/(array_1.length));     console.log(average);

Reverse an Array

let array_1 = [20, 12, 78, 90, 12, 9, 87, 11, 15, 15, 40]; let array_2 = []; for(var i = array_1.length; i>0; i--){       array_2 = array_1[i];       console.log(array_2); }

Remove all Even Number from Array

 let array_1 = [12, 21, 38, 23, 25, 27, 30, 32, 5, 7];     let array_2 = [];     let array_3 = [];     for(var i = 0; i<array_1.length; i++){            if(array_1[i]%2 == 0){                 array_2.push(array_1[i]);            }            else{                array_3.push(array_1[i]);            }     } console.log("Even Numbers Are: "+array_2); console.log("Odd Numbers Are: "+array_3);

Find Median of Values from Array in JavaScript

 let array_1 = [12, 8, 7, 90, 35, 98, 100, 25];     let median;         if(array_1.length %2 == 0){              let array_index = (array_1.length/2);              let new_array_index = (array_index - 1);              median = ((array_1[array_index] + array_1[new_array_index])/2);         }else{              let array_index = ((array_1.length - 1)/2);              median = array_1[array_index];         }     console.log(median);

Find Unique Value from Array in JavaScript

 // Process - 1......... let array_1 = ["A", "C", "B", "B", "G", "D", "A", "R", "C", "M", "D", "A", "R", "C", "B", "D"]; function removeDuplicate(data){   let unique = [];   data.forEach(element => {       if(!unique.includes(element)){           unique.push(element);       }   });   return unique; } console.log(removeDuplicate(array_1)); // Process - 2.................. let array_1 = ["A", "C", "B", "B", "G", "D", "A", "R", "C", "M", "D", "A", "R", "C", "B", "D"]; function removeDuplicate(data){   return [...new Set(data)]; } console.log(removeDuplicate(array_1));

Largest and Lowest Number of the Array using JavaScript

  let array_1 = [7, 9, 3, 2, 0, 10, 90, 21, 12, 87, 11, 22, 8];     let array_2 = [7, 9, 3, 2, 0, 10, 90, 21, 12, 87, 11, 22, 8, 4, 1, 0];         let largest_number = array_1[0];     let lowest_number = array_2[0];     // loop to find largest number of the array..     for(var i=0; i<array_1.length; i++){              if(largest_number<array_1[i]){                    largest_number = array_1[i];              }     }     // loop to find lowest number of the array..     for(var k=0; k<array_2.length; k++){              if(lowest_number > array_2[k]){     ...

Array Implementation in JavaScript

var testScores = [78, 90, 62, 88, 93, 50]; // Decleration of Array.. var highestScore = testScores[4]; // Finding element of Array.. testScores[5] = 71; // Modify the array.. testScores.push(89); // Insert a value at last position.. testScores.unshift(74, 58); // You can also add a new element to the beginning of an array using the unshift() method. testScores.pop(); // This method lets you delete the last element in an array testScores.shift(); // The shift() method to remove the first element of an array. console.log(testScores); console.log("Length of the Array is: "+(testScores.length)); // Print the nmber of elements of the array..

JavaScript Coding Challenge - 4

 /*     Write a program to convert Age to Days (Considering Leap Years) */ ------------------------------------------------------------------------------------------------ function days(birth_year, current_year){       let age;       let leap_years = 0;       let days;       if(birth_year<current_year){          age = (current_year - birth_year);        if(age<=100){          for(var i=birth_year; i<current_year; i++){               if(i%4 ==0 || i%400 ==0){                    leap_years++;               }      ...

JavaScript Coding Challenge - 3

 /*      Write a program to convert minute to second.. */ ------------------------------------------------------------------- function seconds(min){   let second;    if(min > 0){       second = (min * 60)+" Seconds";    }else{       second = "Please Enter valid value";    }        console.log(second); } return seconds(2);

JavaScript Coding Challenge - 2

 /*      Question - Write a program to print A sting in separate letters..      For Eample..      BANANA --> B A N A N A      INDIA --> I N D I A */     ------------------------------------------------------------------------------------ /* Solution-1 */ const iterable = 'banana'; for (const value of iterable) {   console.log(value); } /*      Solution - 2 */ const iterable = new Set([1, 1, 2, 2, 3, 3]); for (const value of iterable) {   console.log(value); }

JavaScript Coding Challenge - 1

  Question - Write a program which returns the number of true values there are in an array.   arrayCount([true, false, false, true, false]) ➞ 2   arrayCount([false, false, false, false]) ➞ 0   arrayCount([]) ➞ 0 ------------------------------------------------------------------------------------   // Solution - 1.. const arr = [true, false, false, false, true]; const count = arr.filter(Boolean).length; // The `.filter(Boolean)` just removes values from a list which are "falsey", like empty strings or null. console.log(count); // Solution - 2....... function arrayCount(arr) {  let result = [];  for(let i = 0; i < arr.length; i++) {      if (arr[i] === true) {          result.push(arr[i]);      }  }  return result.length; } console.log(arrayCount([false,false,true,false,true])); // Solution - 3........ function arrayCount(arr) {       let...