Advertisement
GeorgiLukanov87

Js Front-End - Functions and Statements - Exercises

Feb 26th, 2023 (edited)
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Functions and Statements - Exercises
  2. // https://judge.softuni.org/Contests/Compete/Index/3789#0
  3.  
  4. // -----------------------------------------------------------------------------------------------------
  5. // 01. Smallest of Three Numbers
  6.  
  7. function solve(n1, n2, n3) {
  8.     let minNum = Math.min(n1,n2,n3);
  9.     console.log(minNum)
  10. }
  11.  
  12. // -----------------------------------------------------------------------------------------------------
  13. // 02. Add and Subtract
  14.  
  15. function sum(n1, n2, n3) {
  16.     let result = n1 + n2;
  17.     let final_result = subtract(result, n3)
  18.     console.log(final_result)
  19.    
  20.     function subtract(result, n3) {
  21.         return result - n3
  22.     }
  23.  
  24. }
  25. // -----------------------------------------------------------------------------------------------------
  26. // 03. Characters in Range
  27.  
  28. function solve(char1, char2) {
  29.     let asciiNum1 = char1.charCodeAt(0);
  30.     let asciiNum2 = char2.charCodeAt(0);
  31.     let start = 0;
  32.     let end = 0;
  33.     let result = [];
  34.  
  35.     if (asciiNum1 < asciiNum2) {
  36.         start = asciiNum1;
  37.         end = asciiNum2;
  38.     } else {
  39.         start = asciiNum2;
  40.         end = asciiNum1;
  41.     }
  42.  
  43.     for (let i = start + 1; i < end; i++) {
  44.         let char = String.fromCharCode(i);
  45.         result.push(char)
  46.     }
  47.  
  48.     console.log(...result)
  49. }
  50. // -----------------------------------------------------------------------------------------------------
  51. // 04. Odd And Even Sum
  52.  
  53. function solve(input) {
  54.     let evenSum = 0;
  55.     let oddSum = 0;
  56.     let numAsString = input.toString();
  57.     for (let i = 0; i < numAsString.length; i++) {
  58.         let currNum = Number(numAsString[i]);
  59.         if (currNum % 2 == 0) {
  60.             evenSum += currNum;
  61.         } else {
  62.             oddSum += currNum;
  63.         }
  64.     }
  65.     console.log(`Odd sum = ${oddSum}, Even sum = ${evenSum}`)
  66. }
  67. // -----------------------------------------------------------------------------------------------------
  68. // 05. Palindrome Integers
  69.  
  70. function solve(nums) {
  71.     for (let i = 0; i < nums.length; i++) {
  72.         let currNumber = nums[i];
  73.         let result = isPalindrome(currNumber);
  74.         console.log(result)
  75.     }
  76.  
  77.     function isPalindrome(x){
  78.         if (x < 0) return false
  79.    
  80.         let reversed = 0, y = x
  81.    
  82.         while (y > 0) {
  83.             const lastDigit = y % 10
  84.             reversed = (reversed * 10) + lastDigit
  85.             y = (y / 10) | 0
  86.         }
  87.         return x === reversed
  88.     }
  89. }
  90. // -----------------------------------------------------------------------------------------------------
  91. // 06. Password Validator
  92.  
  93. function passValidator(password) {
  94.     let is_valid = true;
  95.  
  96.     if (password.length < 6 || password.length > 10) {
  97.         console.log("Password must be between 6 and 10 characters")
  98.         is_valid = false;
  99.     }
  100.     if (!/^[A-Za-z0-9]*$/.test(password)) {
  101.         console.log("Password must consist only of letters and digits")
  102.         is_valid = false;
  103.     }
  104.  
  105.     let digits = (onlyLettersAndNumbers(password))
  106.     if (digits < 2) {
  107.         console.log("Password must have at least 2 digits")
  108.         is_valid = false;
  109.     }
  110.  
  111.     function onlyLettersAndNumbers(password) {
  112.         let digitsCounter = 0;
  113.         for (let i = 0; i < password.length; i++) {
  114.             if (/^[0-9]*$/.test(password[i])) {
  115.                 digitsCounter++;
  116.             }
  117.         }
  118.         return digitsCounter;
  119.     }
  120.  
  121.     if (is_valid) {
  122.         console.log("Password is valid")
  123.     }
  124.  
  125.  
  126. }
  127. // -----------------------------------------------------------------------------------------------------
  128. // 07. NxN Matrix
  129.  
  130. function matrix(n) {
  131.     let matrix = [];
  132.     matrix.length = n;
  133.     matrix.fill(n)
  134.  
  135.     for (let i = 0; i < n;i++){
  136.         console.log(...matrix)
  137.     }
  138.    
  139. }
  140. // -----------------------------------------------------------------------------------------------------
  141. // 08. Perfect Number
  142.  
  143. function is_perfect(number) {
  144.     let temp = 0;
  145.     for (let i = 1; i <= number / 2; i++) {
  146.         if (number % i === 0) {
  147.             temp += i;
  148.         }
  149.     }
  150.  
  151.     if (temp === number && temp !== 0) {
  152.         console.log("We have a perfect number!");
  153.     }
  154.     else {
  155.         console.log("It's not so perfect.");
  156.     }
  157. }
  158. // -----------------------------------------------------------------------------------------------------
  159. // 09. Loading Bar
  160.  
  161. function solve(num) {
  162.   let loadBar = "";
  163.   let iterations = Math.floor(num / 10);
  164.  
  165.   for (let i = 0; i < 10; i++) {
  166.       if (i < iterations) {
  167.           loadBar += ('%');
  168.       } else {
  169.           loadBar += ('.')
  170.       }
  171.   }
  172.  
  173.   if (iterations == 10) {
  174.       console.log('100% Complete!')
  175.   }
  176.   console.log(`${iterations}0% ` + "[" + loadBar + ']')
  177.  
  178.   if (iterations < 10) {
  179.       console.log('Still loading...')
  180.   }
  181.  
  182. }
  183. // -----------------------------------------------------------------------------------------------------
  184. // 10. Factorial Division
  185.  
  186. function solve(n1, n2) {
  187.     let result1 = factorial(n1)
  188.     let result2 = factorial(n2)
  189.  
  190.     function factorial(n) {
  191.         var total = 1;
  192.         for (i = 1; i <= n; i++) {
  193.             total = total * i;
  194.         }
  195.         return total;
  196.     }
  197.  
  198.     let factorial_division = result1 / result2
  199.     console.log(factorial_division.toFixed(2))
  200. }
  201.  
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement