diff --git a/array-and-strings.txt b/array-and-strings.txt index 775dd02..0488a6b 100644 --- a/array-and-strings.txt +++ b/array-and-strings.txt @@ -7,7 +7,19 @@ Input: [1, 4, ‘i am a string’, ‘456’] Output: “Numbers: 2, Strings: 2” Solution: - +function countOfStringAndNumber(array) { + let numOfString = 0; + let numOfNumber = 0; + array.forEach(element => { + if (typeof element != "number"){ + numOfString++ + } + else{ + numOfNumber++ + } + }); + console.log("Number:" + numOfNumber, "String:" + numOfString) +} @@ -24,6 +36,16 @@ Output: "monster" Solution: +function longestWord(string) { + let arr = string.split(/[ ,.-]/); + let longest = ""; + arr.forEach(element => { + if (element.length >= longest.length){ + longest = element; + } + }); + return longest; +} @@ -38,7 +60,9 @@ Output: "[]" Solution: - +function Comparison(array,num){ + return array.filter(x => x > num) +} 4) Write a function, which will receive a number between 0 to 999 and spell out that number in English. @@ -51,7 +75,45 @@ Output: “nine thousand four hundred twenty five” Solution: - +function NumToText(num) { + const nums = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] + const nums2 = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] + const nums3 = ["twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"]; + + + if (Math.floor(num / 10) != 0) { + let mat1 = num % 10; + if (Math.floor(num / 100) != 0) { + num = Math.floor(num / 10); + let mat2 = num % 10; + let mat3 = Math.floor(num / 10); + if (mat2 == 0 && mat1 == 0){ + return nums[mat3] + " hundred "; + } + if (mat2 == 0) { + return nums[mat3] + " hundred " + nums[mat1]; + } + if (mat2 == 1) { + return nums[mat3] + " hundred " + nums2[mat1]; + } + if (mat1 == 0){ + return nums[mat3] + " hundred " + nums3[mat2 - 2]; + } + return nums[mat3] + " hundred " + nums3[mat2 - 2] + " " + nums[mat1]; + } + let mat4 = Math.floor(num / 10); + if (mat4 == 1){ + return nums2[mat1] + } + else if (mat1 == 0){ + return nums3[mat4 - 2] + } + else{ + return nums3[mat4 - 2] + " " + nums[mat1] + } + } + return nums[num]; +} 5) A left rotation operation on an array shifts each of the array's elements unit to the left. For example, @@ -59,4 +121,11 @@ if 2 left rotations are performed on array [1, 2, 3, 4, 5], then the array would Solution: - +function rotateWithNum (array, num){ + for (let i = 0; i < num; i++) { + let temp = array[0]; + array.shift(); + array.push(temp); + } + return array; +} diff --git a/array_strings_2.js b/array_strings_2.js index 4873142..86fcb25 100644 --- a/array_strings_2.js +++ b/array_strings_2.js @@ -1,61 +1,185 @@ -1. Enter a number. Reverse its first and last digits. Print the new number. -Input: 13 +1. Enter a number.Reverse its first and last digits.Print the new number. +Input: 13 Output: 31 Input: 895796 Output: 695798 - ---------------- -2. Write down a function which will receive 2 arrays of numbers merge them and return second biggest element of merged array. +function reverseDigits(num) { + let number = num.toString(); + number = number.split(""); + let firstdigit = number[0]; + let lastdigit = number[number.length - 1]; + number[0] = lastdigit; + number[number.length - 1] = firstdigit; + number = number.join(""); + return +number; -Input: [1, 3, 6, 8]; [2, 3, 6, 7] +} +-- -- -- -- -- -- -- - + + +2. Write down a + +function which will receive 2 arrays of numbers merge them and +return second biggest element of merged array. + +Input: [1, 3, 6, 8]; +[2, 3, 6, 7] Output: 7 -Input [1, 4]; [3, 6] +Input[1, 4]; +[3, 6] Output: 4 - ---------------- -3. Write down a function which will receive an array of numbers and remove duplicates from it (using Set) +function secondBiggest(arr1, arr2) { + let newarr = arr1.concat(arr2); + newarr.sort(); + return newarr[newarr.length - 2]; + +} +-- -- -- -- -- -- -- - + + +3. Write down a + +function which will receive an array of numbers and remove duplicates from it(using Set) Input: [1, 3, 5, 2, 5, 2, 6] Output: [1, 3, 5, 2, 6] Input: [1, 1, 1, 3] Output: [1, 3] - ---------------- -4. Create an implementation of the rotational cipher, also sometimes called the Caesar cipher. -The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an -integer key between 0 and 26. Using a key of 0 or 26 will always yield the same output due to modular arithmetic. -The letter is shifted for as many values as the value of the key. +function removeDuplicates(array) { + let set = new Set(array); + let newarr = []; + for (let value of set) { + newarr.push(value) + } + return newarr; +} +-- -- -- -- -- -- -- - + + +4. Create an implementation of the rotational cipher, also sometimes called the Caesar cipher. +The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an +integer key between 0 and 26. Using a key of 0 or 26 will always yield the same output due to modular arithmetic. +The letter is shifted +for as many values as the value of the key. + +1. Encode + +function +Input: abc, 1 +Output: bcd + +Input: Hello world, 7 +Output Olssv dvysk -1. Encode function - Input: abc, 1 - Output: bcd +function encodeCipher(string, num) { + let arr = string.split(" "); + let temp = []; - Input: Hello world, 7 - Output Olssv dvysk + for (let j = 0; j < arr.length; j++) { + let element = arr[j]; + temp[j] = [""] + for (let i = 0; i < element.length; i++) { + let digit = element.charCodeAt(i); + if (digit >= 65 && digit <= 90) { + digit += num; + if (digit > 90) { + digit = digit - 90 + 64; + } else { + temp[j] += String.fromCharCode(digit); + } + } + digit += num; + if (digit > 122) { + digit = digit - 122 + 96; + temp[j] += String.fromCharCode(digit); + } else { + temp[j] += String.fromCharCode(digit); -2. Decode function - Input: Olssv dvysk, 7 - Output: Hello world + } + } + } + return temp.join(" "); +} - Input: bcd, 1 - Output: abc - ---------------- +2. Decode -5. Write down a function which will print the number of the rest seconds until the current day's end. -Example: if current time is 23:59:45 function should print 15 -Example: if current time is 23:50:45 function should print 555 +function +Input: Olssv dvysk, 7 +Output: Hello world ---------------- +Input: bcd, 1 +Output: abc -6. Write down a function which will print week day based on provided day. +function decodeCipher(string, num) { + let arr = string.split(" "); + let temp = []; + + for (let j = 0; j < arr.length; j++) { + let element = arr[j]; + temp[j] = [""] + for (let i = 0; i < element.length; i++) { + let digit = element.charCodeAt(i); + if (digit >= 65 && digit <= 90) { + digit -= num; + if (digit < 65) { + digit = 90 - (65 - digit); + } else { + temp[j] += String.fromCharCode(digit); + } + } else { + digit -= num; + if (digit < 97) { + digit = 122 - (97 - digit) + 1; + temp[j] += String.fromCharCode(digit); + } else { + temp[j] += String.fromCharCode(digit); + + } + } + } + } + return temp.join(" "); +} +-- -- -- -- -- -- -- - + +5. Write down a + +function which will print the number of the rest seconds until the current day 's end. +Example: if current time is 23: 59: 45 + +function should print 15 +Example: if current time is 23: 50: 45 + +function should print 555 + + +function numberOfSeconds() { + let today = new Date(); + let hour = today.getHours(); + let minute = today.getMinutes(); + let sec = today.getSeconds(); + return (23 - hour) * 3600 + (59 - minute) * 60 + (60 - sec); +} +-- -- -- -- -- -- -- - + + +6. Write down a + +function which will print week day based on provided day. Input: "2019/10/14" Output: "Monday" Input: "2014/09/12" Output: "Friday" + +function weekDay(year, month, day) { + let date = new Date(year, month - 1, day) + let wd = date.getDay(); + let weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; + return weekday[wd]; +} \ No newline at end of file