Skip to content

Update array_strings_2.js #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 99 additions & 6 deletions array_strings_2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ Output: 31

Input: 895796
Output: 695798

function reversNumber(a){
let arr = String(a).split("");
for(let i = 0; i< arr.length/2; i++){
let b = arr[i];
arr[i]=arr[arr.length - 1 -i];
arr[arr.length - 1 -i] = b;
}
return Number(arr.join(""));
}
---------------

2. Write down a function which will receive 2 arrays of numbers merge them and return second biggest element of merged array.
Expand All @@ -14,7 +22,14 @@ Output: 7

Input [1, 4]; [3, 6]
Output: 4

function twoArraySecondBiggest(arr1, arr2){
var arr = [];
arr = arr.concat(arr1, arr2);
let max = arr.reduce((p,v)=>((p<v)?v:p))
arr = arr.filter((v)=>(v<max));
max = arr.reduce((p,v)=>((p<v)?v:p));
return max;
}
---------------

3. Write down a function which will receive an array of numbers and remove duplicates from it (using Set)
Expand All @@ -23,7 +38,10 @@ Output: [1, 3, 5, 2, 6]

Input: [1, 1, 1, 3]
Output: [1, 3]

function removeDuplicates(arr){
arr = new Set(arr);
return arr;
}
---------------

4. Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.
Expand All @@ -37,20 +55,89 @@ The letter is shifted for as many values as the value of the key.

Input: Hello world, 7
Output Olssv dvysk

function caesarCipherEncode(str, n){
var a = [];
for(let i = 0; i < str.length; i++){
if(str.charCodeAt(i)>96 && str.charCodeAt(i)<123){
if(str.charCodeAt(i) + n > 122){
a += String.fromCharCode(str.charCodeAt(i) + n - 122 + 96);
}else{
a += String.fromCharCode(str.charCodeAt(i) + n);
}
}else if(str.charCodeAt(i)>64 && str.charCodeAt(i)<91){
if(str.charCodeAt(i) + n > 90){
a += String.fromCharCode(str.charCodeAt(i) + n - 90 + 64);
}else{
a += String.fromCharCode(str.charCodeAt(i) + n);
}
}else{
a += str[i];
}
}
return a;
}
2. Decode function
Input: Olssv dvysk, 7
Output: Hello world

Input: bcd, 1
Output: abc

function caesarCipherDecode(str, n){
var a = [];
for(let i = 0; i < str.length; i++){
if(str.charCodeAt(i)>96 && str.charCodeAt(i)<123){
if(str.charCodeAt(i) - n < 97){
a += String.fromCharCode(str.charCodeAt(i) - n + 122 - 96);
}else{
a += String.fromCharCode(str.charCodeAt(i) - n);
}
}else if(str.charCodeAt(i)>64 && str.charCodeAt(i)<91){
if(str.charCodeAt(i) - n < 65){
a += String.fromCharCode(str.charCodeAt(i) - n + 91 - 65);
}else{
a += String.fromCharCode(str.charCodeAt(i) - n);
}
}else{
a += str[i];
}
}
return a;
}function caesarCipherDecode(str, n){
var a = [];
for(let i = 0; i < str.length; i++){
if(str.charCodeAt(i)>96 && str.charCodeAt(i)<123){
if(str.charCodeAt(i) - n < 97){
a += String.fromCharCode(str.charCodeAt(i) - n + 122 - 96);
}else{
a += String.fromCharCode(str.charCodeAt(i) - n);
}
}else if(str.charCodeAt(i)>64 && str.charCodeAt(i)<91){
if(str.charCodeAt(i) - n < 65){
a += String.fromCharCode(str.charCodeAt(i) - n + 91 - 65);
}else{
a += String.fromCharCode(str.charCodeAt(i) - n);
}
}else{
a += str[i];
}
}
return a;
}
---------------

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 secondsDayEnd(){
var date = new Date();
let h = date.getHours();
let m = date.getMinutes();
let s = date.getSeconds();
h = (23 - h) * 3600;
m = (59 - m) * 60;
s = 60 - s;
return h + m + s;
}
---------------

6. Write down a function which will print week day based on provided day.
Expand All @@ -59,3 +146,9 @@ Output: "Monday"

Input: "2014/09/12"
Output: "Friday"
function dateToDay(str){
const days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var arr = str.split('/');
var date = new Date(arr[0],arr[1] - 1, arr[2]);
return days[date.getDay()];
}