From b3a82c746d27074057a247b4b2041870605fe17b Mon Sep 17 00:00:00 2001 From: laramalkhasyan Date: Fri, 11 Oct 2019 18:54:07 +0400 Subject: [PATCH 1/7] Solutions --- array-and-strings.txt | 65 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/array-and-strings.txt b/array-and-strings.txt index 77d43e5..9b85ce7 100644 --- a/array-and-strings.txt +++ b/array-and-strings.txt @@ -7,7 +7,17 @@ Input: [1, 4, ‘i am a string’, ‘456’] Output: “Numbers: 2, Strings: 2” Solution: - +function printNum(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 +34,15 @@ 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 +57,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 +72,36 @@ 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) { + return nums[mat3] + " hundred " + nums[mat1]; + } + if (mat2 == 1) { + return nums[mat3] + " hundred " + nums2[mat1]; + } + 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 +109,11 @@ if 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; +} From 06d4ddfa092734077973f6b2e554e2ce72f00f36 Mon Sep 17 00:00:00 2001 From: laramalkhasyan Date: Fri, 11 Oct 2019 19:02:47 +0400 Subject: [PATCH 2/7] Updated solution of Ex4 --- array-and-strings.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/array-and-strings.txt b/array-and-strings.txt index 9b85ce7..0359ccd 100644 --- a/array-and-strings.txt +++ b/array-and-strings.txt @@ -84,12 +84,16 @@ function NumToText(num) { 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); @@ -102,6 +106,7 @@ function NumToText(num) { } return nums[num]; +} 5) A left rotation operation on an array shifts each of the array's elements unit to the left. For example, From 7901e1596b21c314cf555345f94c972fb3dc87cf Mon Sep 17 00:00:00 2001 From: laramalkhasyan <43653914+laramalkhasyan@users.noreply.github.com> Date: Mon, 14 Oct 2019 15:50:15 +0400 Subject: [PATCH 3/7] Updated according to the comments --- array-and-strings.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/array-and-strings.txt b/array-and-strings.txt index 0359ccd..4b3b0d0 100644 --- a/array-and-strings.txt +++ b/array-and-strings.txt @@ -7,7 +7,7 @@ Input: [1, 4, ‘i am a string’, ‘456’] Output: “Numbers: 2, Strings: 2” Solution: -function printNum(array) { +function countOfStringAndNumber(array) { let numOfString = 0; let numOfNumber = 0; array.forEach(element => { @@ -34,7 +34,7 @@ Output: "monster" Solution: -function LongestWord(string) { +function longestWord(string) { let arr = string.split(/[ ,.-]/); let longest = ""; arr.forEach(element => { From a2bf240e7a6543cb9564160e2765cd4c433a6293 Mon Sep 17 00:00:00 2001 From: laramalkhasyan <43653914+laramalkhasyan@users.noreply.github.com> Date: Mon, 14 Oct 2019 15:56:23 +0400 Subject: [PATCH 4/7] Update array-and-strings.txt --- array-and-strings.txt | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/array-and-strings.txt b/array-and-strings.txt index 4b3b0d0..93921a7 100644 --- a/array-and-strings.txt +++ b/array-and-strings.txt @@ -11,10 +11,12 @@ function countOfStringAndNumber(array) { let numOfString = 0; let numOfNumber = 0; array.forEach(element => { - if (typeof element != "number") + if (typeof element != "number"){ numOfString++ - else + } + else{ numOfNumber++ + } }); console.log("Number:" + numOfNumber, "String:" + numOfString) } @@ -38,8 +40,9 @@ function longestWord(string) { let arr = string.split(/[ ,.-]/); let longest = ""; arr.forEach(element => { - if (element.length >= longest.length) + if (element.length >= longest.length){ longest = element; + } }); return longest; } @@ -84,26 +87,30 @@ function NumToText(num) { num = Math.floor(num / 10); let mat2 = num % 10; let mat3 = Math.floor(num / 10); - if(mat2 == 0 && mat1 == 0) + 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) + 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) + if (mat4 == 1){ return nums2[mat1] - else if (mat1 == 0) + } + else if (mat1 == 0){ return nums3[mat4 - 2] - else + } + else{ return nums3[mat4 - 2] + " " + nums[mat1] - + } } return nums[num]; } From 9683c11de03aa373b80db2177666230ab13391f8 Mon Sep 17 00:00:00 2001 From: laramalkhasyan <43653914+laramalkhasyan@users.noreply.github.com> Date: Mon, 14 Oct 2019 19:03:34 +0400 Subject: [PATCH 5/7] Create array_strings_2.js --- array_strings_2.js | 180 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 array_strings_2.js diff --git a/array_strings_2.js b/array_strings_2.js new file mode 100644 index 0000000..71d07a1 --- /dev/null +++ b/array_strings_2.js @@ -0,0 +1,180 @@ +1. Enter a number. Reverse its first and last digits. Print the new number. +Input: 13 +Output: 31 + +Input: 895796 +Output: 695798 + +--------------- + +// function reverseDigits(num) { +// let number = num.toString(); +// debugger + +// let firstdigit = number[0]; +// let lastdigit = number[number.length - 1]; +// number = number.replace(number[0],lastdigit); +// console.log(number[0]); +// number = number.replace(number[number.length -1],firstdigit); +// return +number; + +// } +// let dig = reverseDigits(13); +// console.log(dig) + + +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] +Output: 4 + +--------------- + +function secondBiggest(arr1, arr2) { + let newarr = arr1.concat(arr2); + newarr.sort(); + return newarr[newarr.length - 2]; + +} +let s = secondBiggest([1, 3, 6, 8], [2, 3, 6, 7]); + +console.log(s); + +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] + +--------------- +function removeDuplicates(array) { + let set = new Set(array); + let newarr = []; + for (let value of set) { + newarr.push(value) + } + console.log(newarr); +} + +removeDuplicates([1, 3, 5, 2, 5, 2, 6]); +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 + +function encodeCipher(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 > 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); + + } + } + } + return temp.join(" "); +} +let a = encodeCipher("abc", 1) + +console.log(a); + Input: Hello world, 7 + Output Olssv dvysk + +2. Decode function + Input: Olssv dvysk, 7 + Output: Hello world + + Input: bcd, 1 + Output: abc + +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(" "); +} +let b = decodeCipher("Olssv dvysk", 7) + +console.log(b); +--------------- + +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 endoftheday = new Date() +} + +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]; +} + +let d = weekDay(2014, 9, 12); +console.log(d) From d2f724b948533d14da4408214394034e311079ef Mon Sep 17 00:00:00 2001 From: laramalkhasyan <43653914+laramalkhasyan@users.noreply.github.com> Date: Mon, 14 Oct 2019 23:23:34 +0400 Subject: [PATCH 6/7] Delete array_strings_2.js --- array_strings_2.js | 180 --------------------------------------------- 1 file changed, 180 deletions(-) delete mode 100644 array_strings_2.js diff --git a/array_strings_2.js b/array_strings_2.js deleted file mode 100644 index 71d07a1..0000000 --- a/array_strings_2.js +++ /dev/null @@ -1,180 +0,0 @@ -1. Enter a number. Reverse its first and last digits. Print the new number. -Input: 13 -Output: 31 - -Input: 895796 -Output: 695798 - ---------------- - -// function reverseDigits(num) { -// let number = num.toString(); -// debugger - -// let firstdigit = number[0]; -// let lastdigit = number[number.length - 1]; -// number = number.replace(number[0],lastdigit); -// console.log(number[0]); -// number = number.replace(number[number.length -1],firstdigit); -// return +number; - -// } -// let dig = reverseDigits(13); -// console.log(dig) - - -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] -Output: 4 - ---------------- - -function secondBiggest(arr1, arr2) { - let newarr = arr1.concat(arr2); - newarr.sort(); - return newarr[newarr.length - 2]; - -} -let s = secondBiggest([1, 3, 6, 8], [2, 3, 6, 7]); - -console.log(s); - -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] - ---------------- -function removeDuplicates(array) { - let set = new Set(array); - let newarr = []; - for (let value of set) { - newarr.push(value) - } - console.log(newarr); -} - -removeDuplicates([1, 3, 5, 2, 5, 2, 6]); -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 - -function encodeCipher(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 > 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); - - } - } - } - return temp.join(" "); -} -let a = encodeCipher("abc", 1) - -console.log(a); - Input: Hello world, 7 - Output Olssv dvysk - -2. Decode function - Input: Olssv dvysk, 7 - Output: Hello world - - Input: bcd, 1 - Output: abc - -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(" "); -} -let b = decodeCipher("Olssv dvysk", 7) - -console.log(b); ---------------- - -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 endoftheday = new Date() -} - -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]; -} - -let d = weekDay(2014, 9, 12); -console.log(d) From 56ba47b0da27ada7208c40fad56af70e0d12169d Mon Sep 17 00:00:00 2001 From: Lara Date: Mon, 14 Oct 2019 23:51:21 +0400 Subject: [PATCH 7/7] Solutions --- array_strings_2.js | 190 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 157 insertions(+), 33 deletions(-) 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