Skip to content

Commit 51284f8

Browse files
committed
Add the .js files
0 parents  commit 51284f8

16 files changed

+349
-0
lines changed

caesarsCipher.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipher the meanings of the letters are shifted by some set amount.
2+
//
3+
// A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on.
4+
//
5+
// Write a function which takes a ROT13 encoded string as input and returns a decoded string.
6+
//
7+
// All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.
8+
9+
function rot13(str) {
10+
var strLength = str.length;
11+
var count = 0;
12+
var result = "";
13+
var check;
14+
15+
while(count < strLength) {
16+
check = 0;
17+
if (str.charCodeAt(count) < 65 || str.charCodeAt(count) > 90) {
18+
result = result.concat(str[count]);
19+
} else {
20+
check = str.charCodeAt(count) + 13;
21+
if (check > 90) {
22+
check = str.charCodeAt(count) - 13;
23+
result = result.concat(String.fromCharCode(check));
24+
} else {
25+
result = result.concat(String.fromCharCode(check));
26+
}
27+
}
28+
count++;
29+
}
30+
31+
return result;
32+
}
33+
34+
console.log(rot13("SERR PBQR PNZC")); // "FREE CODE CAMP"

checkPalindromes.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Return true if the given string is a palindrome. Otherwise, return false.
2+
//
3+
// A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.
4+
//
5+
// Note
6+
// You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything lower case in order to check for palindromes.
7+
//
8+
// We'll pass strings with varying formats, such as "racecar", "RaceCar", and "race CAR" among others.
9+
//
10+
// We'll also pass strings with special symbols, such as "2A3*3a2", "2A3 3a2", and "2_A3*3#A2".
11+
12+
13+
function palindrome(str) {
14+
str = str.toLowerCase();
15+
str = str.replace(/[^a-z0-9]/g, '');
16+
var strArr = str.split('');
17+
var lenOfStrArr = strArr.length;
18+
19+
for (var i = 0, j = lenOfStrArr - 1; i < lenOfStrArr / 2; i++, j--) {
20+
if (strArr[i] != strArr[j]) {
21+
return false;
22+
}
23+
}
24+
return true;
25+
}
26+
27+
28+
29+
console.log(palindrome(" eye"));

chunkyMonkey.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// 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.
2+
3+
4+
5+
function chunkArrayInGroups(arr, size) {
6+
var counter = 0;
7+
var resultArr = [];
8+
9+
while (counter < arr.length) {
10+
resultArr.push(arr.slice(counter, counter += size));
11+
}
12+
13+
return resultArr;
14+
}
15+
16+
console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));

confirmTheEnding.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Check if a string (first argument, str) ends with the given target string (second argument, target).
2+
//
3+
// This challenge can be solved with the .endsWith() method, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.
4+
5+
function confirmEnding(str, target) {
6+
var lenOfTarget = target.length;
7+
var strEnding = str.slice(-lenOfTarget);
8+
var targetArr = target.split('');
9+
var strEndingArr = strEnding.split('');
10+
11+
for(var i = 0; i < lenOfTarget; i++) {
12+
if (strEndingArr[i] != targetArr[i]) {
13+
return false;
14+
}
15+
}
16+
17+
return true;
18+
}
19+
20+
console.log(confirmEnding("Bastian", "an"));

factorializeANumber.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Return the factorial of the provided integer.
2+
//
3+
// If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.
4+
//
5+
// Factorials are often represented with the shorthand notation n!
6+
//
7+
// For example: 5! = 1 * 2 * 3 * 4 * 5 = 120
8+
9+
function factorialize(num) {
10+
if (num === 0) {
11+
return 1;
12+
}
13+
return factorialize(num - 1) * num;
14+
}
15+
16+
console.log(factorialize(5));

falsyBounce.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Remove all falsy values from an array.
2+
//
3+
// Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.
4+
//
5+
6+
function bouncer(arr) {
7+
return arr.filter(Boolean);
8+
}
9+
10+
console.log(bouncer([7, "ate", "", false, NaN, , "false", 9]));

findTheLongestWordInAString.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Return the length of the longest word in the provided sentence.
2+
//
3+
// Your response should be a number.
4+
5+
6+
function findLongestWord(str) {
7+
var strArr = str.split(' ');
8+
var lenOfStrArr = strArr.length;
9+
var max = 0;
10+
11+
for (var i = 0; i < lenOfStrArr; i++) {
12+
if(strArr[i].length > max) {
13+
max = strArr[i].length;
14+
15+
}
16+
}
17+
18+
return max;
19+
}
20+
21+
console.log(findLongestWord("The quick brown fox jumped over the lazy dog"));

mutations.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 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.
2+
//
3+
// For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case.
4+
//
5+
// The arguments ["hello", "hey"] should return false because the string "hello" does not contain a "y".
6+
//
7+
// Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien".
8+
9+
10+
function mutation(arr) {
11+
var input = arr[0].toLowerCase();
12+
var check = arr[1].toLowerCase();
13+
14+
var inputArr = input.split('');
15+
var checkArr = check.split('');
16+
17+
var validate = true;
18+
19+
for (var i = 0; i < checkArr.length; i++) {
20+
validate = false;
21+
for (var j = 0; j < inputArr.length; j++) {
22+
if(checkArr[i] == inputArr[j]) {
23+
validate = true;
24+
break;
25+
} else {
26+
validate = false;
27+
}
28+
}
29+
30+
if (!validate) {
31+
return false;
32+
}
33+
}
34+
35+
return true;
36+
37+
}
38+
39+
console.log(mutation(["hello", "hel"]));

repeatAString.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Repeat a given string (first argument) num times (second argument). Return an empty string if num is not a positive number.
2+
3+
4+
function repeatStringNumTimes(str, num) {
5+
var resultArr = [];
6+
7+
for (var i = 0; i < num; i++) {
8+
resultArr.push(str);
9+
}
10+
return resultArr.join("");
11+
12+
}
13+
14+
console.log(repeatStringNumTimes("abc", 3));

returnLargestNumbersInArray.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.
2+
//
3+
// Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].
4+
5+
6+
7+
function largestOfFour(arr) {
8+
var max;
9+
var resultArr = [];
10+
11+
for (var outerArr = 0; outerArr < arr.length; outerArr++) {
12+
max = 0;
13+
for (var innerArr = 0; innerArr < arr[outerArr].length; innerArr++) {
14+
if (arr[outerArr][innerArr] > max) {
15+
max = arr[outerArr][innerArr];
16+
}
17+
}
18+
resultArr.push(max);
19+
}
20+
return resultArr;
21+
}
22+
23+
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));

reverseAString.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function reverseString(str) {
2+
var strArr = str.split('');
3+
strArr.reverse();
4+
var result = strArr.join('');
5+
return result;
6+
}
7+
8+
console.log(reverseString("hello"));

seekAndDestroy.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.
2+
function destroyer(arr) {
3+
var initialArr = arguments[0];
4+
var argsLength = arguments.length;
5+
6+
var checkArr = [];
7+
8+
var counter = 1;
9+
while(counter < argsLength) {
10+
checkArr.push(arguments[counter]);
11+
counter++;
12+
}
13+
14+
function remove(val) {
15+
var i = 0;
16+
var valid = true;
17+
while(i < checkArr.length) {
18+
if (val !== checkArr[i]) {
19+
valid = true;
20+
} else {
21+
return false;
22+
}
23+
i++;
24+
}
25+
return valid;
26+
}
27+
28+
var result = initialArr.filter(remove);
29+
return result;
30+
31+
}
32+
33+
console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));

slasherFlick.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Return the remaining elements of an array after chopping off n elements from the head.
2+
//
3+
// The head means the beginning of the array, or the zeroth index.
4+
5+
6+
function slasher(arr, howMany) {
7+
var resultArr = [];
8+
var counter = 0;
9+
10+
while (counter < arr.length) {
11+
if(counter < howMany) {
12+
counter++;
13+
} else {
14+
resultArr.push(arr[counter]);
15+
counter++;
16+
}
17+
}
18+
19+
return resultArr;
20+
}
21+
22+
console.log(slasher([1, 2, 3], 4));

titleCaseASentence.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.
2+
//
3+
// For the purpose of this exercise, you should also capitalize connecting words like "the" and "of".
4+
5+
6+
function titleCase(str) {
7+
str = str.toLowerCase();
8+
var strArr = str.split(" ");
9+
var lenOfStrArr = strArr.length;
10+
var resultArr = [];
11+
12+
for(var i = 0; i < lenOfStrArr; i++) {
13+
resultArr[i] = strArr[i][0].toUpperCase() + strArr[i].slice(1);
14+
}
15+
16+
return result = resultArr.join(" ");
17+
}
18+
19+
console.log(titleCase("I'm a little tea pot"));

truncateAString.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.
2+
//
3+
// Note that inserting the three dots to the end will add to the string length.
4+
//
5+
// However, if the given maximum string length num is less than or equal to 3, then the addition of the three dots does not add to the string length in determining the truncated string.
6+
7+
8+
9+
function truncateString(str, num) {
10+
if (num < str.length && num > 3) {
11+
return str.slice(0, num - 3) + "...";
12+
} else if (num >= str.length && num > 3) {
13+
return str;
14+
} else {
15+
return str.slice(0, num) + "...";
16+
}
17+
}
18+
19+
console.log(truncateString("Absolutely Longer", 2));

whereDoIBelong.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.
2+
//
3+
// For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).
4+
//
5+
// Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).
6+
7+
8+
function getIndexToIns(arr, num) {
9+
10+
// sort arr in ascending order
11+
arr.sort(function(a, b) {
12+
return a - b;
13+
});
14+
15+
var counter = 0;
16+
while (counter < arr.length) {
17+
if (num > arr[counter]) {
18+
counter++;
19+
} else {
20+
return counter;
21+
}
22+
}
23+
return arr.length;
24+
}
25+
26+
console.log(getIndexToIns([10, 20, 30, 40, 50], 35)); // 3

0 commit comments

Comments
 (0)