From 1f5b70236dbac053737a7ea2698c0ec3159a8860 Mon Sep 17 00:00:00 2001 From: Harsh Sharma Date: Sun, 16 Apr 2023 17:38:03 +0530 Subject: [PATCH] Preogess Level 2 Day 5 --- Solutions by devHarsh007/DAY_5.js | 127 +++++++++++++++++++++++++++--- 1 file changed, 114 insertions(+), 13 deletions(-) diff --git a/Solutions by devHarsh007/DAY_5.js b/Solutions by devHarsh007/DAY_5.js index b6f55b0..fee1d95 100644 --- a/Solutions by devHarsh007/DAY_5.js +++ b/Solutions by devHarsh007/DAY_5.js @@ -116,9 +116,14 @@ const findMax = (a, b, c) => { // =============================================================================================================================================== -// Linear equation is calculated as follows: ax + by + c = 0. Write a function which calculates value of a linear equation, solveLinEquation. + +// Linear equation is calculated as follows: ax + by + c = 0. Write a function which calculates value of a linear equation, solveLinEquation. +const solveLinEquation = () => { + return (-c - by) / a; +} // Quadratic equation is calculated as follows: ax2 + bx + c = 0. Write a function which calculates value or values of a quadratic equation, solveQuadEquation. +// same as before // console.log(solveQuadratic()) // {0} // console.log(solveQuadratic(1, 4, 4)) // {-2} // console.log(solveQuadratic(1, -1, -2)) // {2, -1} @@ -126,47 +131,143 @@ const findMax = (a, b, c) => { // console.log(solveQuadratic(1, 0, -4)) //{2, -2} // console.log(solveQuadratic(1, -1, 0)) //{1, 0} -// Declare a function name printArray.It takes array as a parameter and it prints out each value of the array. - -// Write a function name showDateTime which shows time in this format: 08 / 01 / 2020 04: 08 using the Date object. +// Declare a function name printArray. It takes array as a parameter and it prints out each value of the array. +const printArray = (arr) => { + for (let i of arr) { + console.log(i + "\n"); + } +} +// Write a function name showDateTime which shows time in this format: 08/01/2020 04:08 using the Date object. +const showDateTime = () => { + const dates = new Date(); +} // showDateTime() -// 08 / 01 / 2020 04: 08 - -// Declare a function name swapValues.This function swaps value of x to y. +// 08/01/2020 04:08 + +// Declare a function name swapValues. This function swaps value of x to y. +const swapValues = (x, y) => { + x = x + y; + y = x - y; + x = x - y; + return (x, y); +} // swapValues(3, 4) // x => 4, y=>3 // swapValues(4, 5) // x = 5, y = 4 -// Declare a function name reverseArray.It takes array as a parameter and it returns the reverse of the array(don't use method). -// console.log(reverseArray([1, 2, 3, 4, 5])) -// //[5, 4, 3, 2, 1] -// console.log(reverseArray(['A', 'B', 'C'])) -// //['C', 'B', 'A'] +// Declare a function name reverseArray. It takes array as a parameter and it returns the reverse of the array (don't use method). +const reverseArray = (arr) => { + output = Array(); + for (let i of arr) { + output.unshift(i); + } + return output; +} // Declare a function name capitalizeArray.It takes array as a parameter and it returns the - capitalizedarray. +const capitalizedArray = (arr) => { + let output = []; + for (let i of arr) { + output.push(i.toUpperCase()); + } + return output; +} // Declare a function name addItem.It takes an item parameter and it returns an array after adding the item +function addItem(...item) { + let output = []; + for (let i of item) { + output.push(i); + } + return output; +} // Declare a function name removeItem.It takes an index parameter and it returns an array after removing an item +function removeItem(arr, index) { + arr.removeItem(arr.charAt(index)); +} // Declare a function name sumOfNumbers.It takes a number parameter and it adds all the numbers in that range. +function sumOfNumbers(num) { + let sum = 0; + for (let i = 1; i <= num; i++) { + sum += i; + } + return sum; +} // Declare a function name sumOfOdds.It takes a number parameter and it adds all the odd numbers in that - range. +function sumOfOdds(num) { + let sum = 0; + for (let i = 1; i <= num; i++) { + if (i % 2 != 0) { + sum += i; + } + } + return sum; +} // Declare a function name sumOfEven.It takes a number parameter and it adds all the even numbers in that - range. +function sumOfEven(num) { + let sum = 0; + for (let i = 1; i <= num; i++) { + if (i % 2 === 0) { + sum += i; + } + } + return sum; +} // Declare a function name evensAndOdds.It takes a positive integer as parameter and it counts number of evens and odds in the number. // evensAndOdds(100); // The number of odds are 50. // The number of evens are 51. +function evensAndOdds(num) { + let numOdd = 0; + let numEven = 0; + let sumEven = 0; + let sumOdd = 0; + for (let i = 1; i <= num; i++) { + if (i % 2 === 0) { + sumEven += i; + numEven += 1; + } + else { + sumOdd += i; + numOdd += 1; + } + } + let statmentOdd = `The number of Odds are ${numOdd}`; + let statementEven = `The number of Evens are ${numEven}`; +} // Write a function which takes any number of arguments and return the sum of the arguments // sum(1, 2, 3) // -> 6 // sum(1, 2, 3, 4) // -> 10 +function sumOfArgs(...nums) { + let sum = 0; + for (let i of nums) { + sum += i; + } + return sum; +} + +// Write a function which generates a randomUserIp. +function genRandnum() { + return Math.floor(Math.random() * 11); +} +function RandomUserIp() { + let ipArr = []; + for (let i = 0; i < 4; i++) { + ipArr.push(Math.floor(Math.random() * 256)) + } + let ip = ipArr.join("."); + return ip; -// Writ a function which generates a randomUserIp. +} // Write a function which generates a randomMacAddress + // Declare a function name randomHexaNumberGenerator.When this function is called it generates a random hexadecimal number.The function return the hexadecimal number. // console.log(randomHexaNumberGenerator());