Pages

Showing posts with label basic algorithm. Show all posts
Showing posts with label basic algorithm. Show all posts

Wednesday, March 9, 2016

CAESARS CIPHER

In this challenge we have to decode a cipher message. More about cipher can be found out here.

So we have a simplest and most widely known ciphers called Caesar cipher, also called shift cipher. Basically in a shift cipher the meanings of the letters are shifted by some set value.

An example of this type of modern cipher is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on.

So we have to write a function which takes a ROT13 encoded string as input and returns a decoded string.

Other conditions are:-

1.  All letters should be uppercase.

2.  Non-alphabetic character i.e. spaces and punctuation's are not to be transformed, but have to be passed on.

At last the list of clues in the form of helpful links are as follows:
It took me very less time to implement the algorithm. In this algorithm I loop thorough all the string elements converting each of them to equivalent uni-code values and then pushing them to the array. Finally I joined all the elements using the join() method of arrays.

                 function rot13(str) { // LBH QVQ VG!
                 arr = [];
                 for (var i = 0; i < str.length; i++){
                     var a = str.charCodeAt(i);
                     if(a >= 65 && a <= 77){
                         a += 13;
                     } else if(a >= 78 && a <= 90){
                         a -= 13;
                     } else {
                       a = a;
                     }
                     var c = String.fromCharCode(a);
                     arr.push(c);
                }
            }

Tuesday, March 8, 2016

WHERE DO I BELONG

This challenge requires returning the lowest index at which a value  that is second argument should be inserted into an array which is the first argument after sorting it.

The algorithm was quite straight forward, only thing that made me to think was what if the number is to inserted at index greater than last index of the array e.



Sunday, March 6, 2016

SEEK AND DESTROY

In this challenge we are provided with an initial array i.e the first argument in the destroyer function It is followed by one or more arguments. We have to remove all elements from the first array that are of the same value as are these arguments.

Friday, March 4, 2016

FALSY BOUNCER

In this challenge we have to remove all falsy values from an array.

Falsy values in JavaScript are:-

 false, null, 0, "", undefined, and NaN.

For this challenge I went through the clues that are given in the form of clickable links above, again leading to MDN JavaScript documentation.

Then on with little bit of tweaking I was able to implement the under-mentioned solution for the challenge.

So, here is my solution.

                function bouncer(arr) {
             function Boolean(value){
                 var i = 0;
                 arr1 = [NaN, 0, false, null, undefined];
                 if (arr1[i] === value){
                     i++;
                 } 

                 return value;
             }
             return arr.filter(Boolean);
           }

Thursday, March 3, 2016

MUTATIONS

This Challenge requires to return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.

For example, ["hello", "Hello"], have to return true because all of the letters in the second string are present in the first, after ignoring case.
The arguments ["hello", "hey"] should have to return false because the string in first element "hello" does not contain a "y".

Similarly, ["Alien", "line"], should be returning true because all of the letters in "line" are there in "Alien".

Wednesday, March 2, 2016

SLASHER FLICK

In this challenge I have to return the remaining elements of an array after chopping off n elements from the head.

The head means the beginning of the array, or the zeroth index.

CHUNKY MONKEY

Challenge this time is to write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.