Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Functions and Statements - Exercises
- // https://judge.softuni.org/Contests/Compete/Index/3789#0
- // -----------------------------------------------------------------------------------------------------
- // 01. Smallest of Three Numbers
- function solve(n1, n2, n3) {
- let minNum = Math.min(n1,n2,n3);
- console.log(minNum)
- }
- // -----------------------------------------------------------------------------------------------------
- // 02. Add and Subtract
- function sum(n1, n2, n3) {
- let result = n1 + n2;
- let final_result = subtract(result, n3)
- console.log(final_result)
- function subtract(result, n3) {
- return result - n3
- }
- }
- // -----------------------------------------------------------------------------------------------------
- // 03. Characters in Range
- function solve(char1, char2) {
- let asciiNum1 = char1.charCodeAt(0);
- let asciiNum2 = char2.charCodeAt(0);
- let start = 0;
- let end = 0;
- let result = [];
- if (asciiNum1 < asciiNum2) {
- start = asciiNum1;
- end = asciiNum2;
- } else {
- start = asciiNum2;
- end = asciiNum1;
- }
- for (let i = start + 1; i < end; i++) {
- let char = String.fromCharCode(i);
- result.push(char)
- }
- console.log(...result)
- }
- // -----------------------------------------------------------------------------------------------------
- // 04. Odd And Even Sum
- function solve(input) {
- let evenSum = 0;
- let oddSum = 0;
- let numAsString = input.toString();
- for (let i = 0; i < numAsString.length; i++) {
- let currNum = Number(numAsString[i]);
- if (currNum % 2 == 0) {
- evenSum += currNum;
- } else {
- oddSum += currNum;
- }
- }
- console.log(`Odd sum = ${oddSum}, Even sum = ${evenSum}`)
- }
- // -----------------------------------------------------------------------------------------------------
- // 05. Palindrome Integers
- function solve(nums) {
- for (let i = 0; i < nums.length; i++) {
- let currNumber = nums[i];
- let result = isPalindrome(currNumber);
- console.log(result)
- }
- function isPalindrome(x){
- if (x < 0) return false
- let reversed = 0, y = x
- while (y > 0) {
- const lastDigit = y % 10
- reversed = (reversed * 10) + lastDigit
- y = (y / 10) | 0
- }
- return x === reversed
- }
- }
- // -----------------------------------------------------------------------------------------------------
- // 06. Password Validator
- function passValidator(password) {
- let is_valid = true;
- if (password.length < 6 || password.length > 10) {
- console.log("Password must be between 6 and 10 characters")
- is_valid = false;
- }
- if (!/^[A-Za-z0-9]*$/.test(password)) {
- console.log("Password must consist only of letters and digits")
- is_valid = false;
- }
- let digits = (onlyLettersAndNumbers(password))
- if (digits < 2) {
- console.log("Password must have at least 2 digits")
- is_valid = false;
- }
- function onlyLettersAndNumbers(password) {
- let digitsCounter = 0;
- for (let i = 0; i < password.length; i++) {
- if (/^[0-9]*$/.test(password[i])) {
- digitsCounter++;
- }
- }
- return digitsCounter;
- }
- if (is_valid) {
- console.log("Password is valid")
- }
- }
- // -----------------------------------------------------------------------------------------------------
- // 07. NxN Matrix
- function matrix(n) {
- let matrix = [];
- matrix.length = n;
- matrix.fill(n)
- for (let i = 0; i < n;i++){
- console.log(...matrix)
- }
- }
- // -----------------------------------------------------------------------------------------------------
- // 08. Perfect Number
- function is_perfect(number) {
- let temp = 0;
- for (let i = 1; i <= number / 2; i++) {
- if (number % i === 0) {
- temp += i;
- }
- }
- if (temp === number && temp !== 0) {
- console.log("We have a perfect number!");
- }
- else {
- console.log("It's not so perfect.");
- }
- }
- // -----------------------------------------------------------------------------------------------------
- // 09. Loading Bar
- function solve(num) {
- let loadBar = "";
- let iterations = Math.floor(num / 10);
- for (let i = 0; i < 10; i++) {
- if (i < iterations) {
- loadBar += ('%');
- } else {
- loadBar += ('.')
- }
- }
- if (iterations == 10) {
- console.log('100% Complete!')
- }
- console.log(`${iterations}0% ` + "[" + loadBar + ']')
- if (iterations < 10) {
- console.log('Still loading...')
- }
- }
- // -----------------------------------------------------------------------------------------------------
- // 10. Factorial Division
- function solve(n1, n2) {
- let result1 = factorial(n1)
- let result2 = factorial(n2)
- function factorial(n) {
- var total = 1;
- for (i = 1; i <= n; i++) {
- total = total * i;
- }
- return total;
- }
- let factorial_division = result1 / result2
- console.log(factorial_division.toFixed(2))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement