done palindrome and caesar challenge
This commit is contained in:
parent
c54951b9b8
commit
0513964dd5
|
@ -1,5 +1,50 @@
|
||||||
const caesar = function() {
|
const caesar = function(word, shift) {
|
||||||
|
let alphabet = [["A","a"],["B","b"],["C","c"],["D","d"],["E","e"],["F","f"],["G","g"],["H","h"],["I","i"],["J","j"],["K","k"],["L","l"],["M","m"],["N","n"],["O","o"],["P","p"],["Q","q"],["R","r"],["S","s"],["T","t"],["U","u"],["V","v"],["W","w"],["X","x"],["Y","y"],["Z","z"]];
|
||||||
|
let cypher = [["A","a"],["B","b"],["C","c"],["D","d"],["E","e"],["F","f"],["G","g"],["H","h"],["I","i"],["J","j"],["K","k"],["L","l"],["M","m"],["N","n"],["O","o"],["P","p"],["Q","q"],["R","r"],["S","s"],["T","t"],["U","u"],["V","v"],["W","w"],["X","x"],["Y","y"],["Z","z"]];
|
||||||
|
|
||||||
|
|
||||||
|
let newWord = [];
|
||||||
|
|
||||||
|
if (shift > 0) {
|
||||||
|
//if positive number, shift and push
|
||||||
|
for (i = 0; i < shift; i++) {
|
||||||
|
//cypher.unshift(cypher.pop());
|
||||||
|
cypher.push(cypher.shift());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//if negative number, turns to positive, pop and unshift
|
||||||
|
for (i = 0; i < Math.abs(shift); i++) {
|
||||||
|
cypher.unshift(cypher.pop());
|
||||||
|
//cypher.push(cypher.shift());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("alphabet : " + alphabet);
|
||||||
|
console.log("cypherla : " + cypher);
|
||||||
|
|
||||||
|
for (j = 0; j < word.length; j++) {
|
||||||
|
let char1 = word.charAt(j);
|
||||||
|
console.log(char1);
|
||||||
|
for (k = 0; k < alphabet.length; k++) {
|
||||||
|
//let char2 = alphabet[k];
|
||||||
|
if (char1 === alphabet[k][0]) {
|
||||||
|
newWord.push(cypher[k][0]);
|
||||||
|
break;
|
||||||
|
} else if (char1 === alphabet[k][1]) {
|
||||||
|
newWord.push(cypher[k][1]);
|
||||||
|
break;
|
||||||
|
} else if (char1 === ',' || char1 === ' ' | char1 === '!') {
|
||||||
|
newWord.push(char1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(newWord);
|
||||||
|
let lastWord = newWord.join(""); //join array elements to a string without any separator ""
|
||||||
|
console.log(lastWord);
|
||||||
|
return lastWord;
|
||||||
|
// console.log(lastWord.replace(/,/g, ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = caesar
|
module.exports = caesar
|
||||||
|
|
|
@ -4,22 +4,22 @@ describe('caesar', function() {
|
||||||
it('works with single letters', function() {
|
it('works with single letters', function() {
|
||||||
expect(caesar('A', 1)).toEqual('B');
|
expect(caesar('A', 1)).toEqual('B');
|
||||||
});
|
});
|
||||||
xit('works with words', function() {
|
it('works with words', function() {
|
||||||
expect(caesar('Aaa', 1)).toEqual('Bbb');
|
expect(caesar('Aaa', 1)).toEqual('Bbb');
|
||||||
});
|
});
|
||||||
xit('works with phrases', function() {
|
it('works with phrases', function() {
|
||||||
expect(caesar('Hello, World!', 5)).toEqual('Mjqqt, Btwqi!');
|
expect(caesar('Hello, World!', 5)).toEqual('Mjqqt, Btwqi!');
|
||||||
});
|
});
|
||||||
xit('works with negative shift', function() {
|
it('works with negative shift', function() {
|
||||||
expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!');
|
expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!');
|
||||||
});
|
});
|
||||||
xit('wraps', function() {
|
it('wraps', function() {
|
||||||
expect(caesar('Z', 1)).toEqual('A');
|
expect(caesar('Z', 1)).toEqual('A');
|
||||||
});
|
});
|
||||||
xit('works with large shift factors', function() {
|
it('works with large shift factors', function() {
|
||||||
expect(caesar('Hello, World!', 75)).toEqual('Ebiil, Tloia!');
|
expect(caesar('Hello, World!', 75)).toEqual('Ebiil, Tloia!');
|
||||||
});
|
});
|
||||||
xit('works with large negative shift factors', function() {
|
it('works with large negative shift factors', function() {
|
||||||
expect(caesar('Hello, World!', -29)).toEqual('Ebiil, Tloia!');
|
expect(caesar('Hello, World!', -29)).toEqual('Ebiil, Tloia!');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
|
<title>Document</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<script src="test.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1,52 @@
|
||||||
|
function caesar(word, shift) {
|
||||||
|
//let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
|
||||||
|
|
||||||
|
let alphabet = [["A","a"],["B","b"],["C","c"],["D","d"],["E","e"],["F","f"],["G","g"],["H","h"],["I","i"],["J","j"],["K","k"],["L","l"],["M","m"],["N","n"],["O","o"],["P","p"],["Q","q"],["R","r"],["S","s"],["T","t"],["U","u"],["V","v"],["W","w"],["X","x"],["Y","y"],["Z","z"]];
|
||||||
|
let cypher = [["A","a"],["B","b"],["C","c"],["D","d"],["E","e"],["F","f"],["G","g"],["H","h"],["I","i"],["J","j"],["K","k"],["L","l"],["M","m"],["N","n"],["O","o"],["P","p"],["Q","q"],["R","r"],["S","s"],["T","t"],["U","u"],["V","v"],["W","w"],["X","x"],["Y","y"],["Z","z"]];
|
||||||
|
|
||||||
|
// declare an array for the secret word
|
||||||
|
let newWord = [];
|
||||||
|
|
||||||
|
if (shift > 0) {
|
||||||
|
//if positive number, shift and push
|
||||||
|
for (i = 0; i < shift; i++) {
|
||||||
|
//cypher.unshift(cypher.pop());
|
||||||
|
cypher.push(cypher.shift());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//if negative number, turns to positive, pop and unshift
|
||||||
|
for (i = 0; i < Math.abs(shift); i++) {
|
||||||
|
cypher.unshift(cypher.pop());
|
||||||
|
//cypher.push(cypher.shift());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("alphabet : " + alphabet);
|
||||||
|
console.log("cypherla : " + cypher);
|
||||||
|
|
||||||
|
// read each character from word one by one
|
||||||
|
for (j = 0; j < word.length; j++) {
|
||||||
|
let char1 = word.charAt(j);
|
||||||
|
console.log(char1);
|
||||||
|
for (k = 0; k < alphabet.length; k++) {
|
||||||
|
//let char2 = alphabet[k];
|
||||||
|
if (char1 === alphabet[k][0]) { // if in capital letter
|
||||||
|
newWord.push(cypher[k][0]);
|
||||||
|
break;
|
||||||
|
} else if (char1 === alphabet[k][1]) { // if in small letter
|
||||||
|
newWord.push(cypher[k][1]);
|
||||||
|
break;
|
||||||
|
} else if (char1 === ',' || char1 === ' ' | char1 === '!') { // maintain comma, space and exclamation
|
||||||
|
newWord.push(char1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(newWord);
|
||||||
|
let lastWord = newWord.join(""); //join array elements to a string without any separator ""
|
||||||
|
console.log(lastWord);
|
||||||
|
// console.log(lastWord.replace(/,/g, ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
caesar("Hello, world!", 5);
|
|
@ -1,25 +1,85 @@
|
||||||
function add () {
|
function add (num1, num2) {
|
||||||
|
let value1 = num1;
|
||||||
|
let value2 = num2;
|
||||||
|
let total = num1 + num2;
|
||||||
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
function subtract () {
|
function subtract (num1, num2) {
|
||||||
|
let value1 = num1;
|
||||||
|
let value2 = num2;
|
||||||
|
let total = num1 - num2;
|
||||||
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
function sum () {
|
function sum (numbersArray) {
|
||||||
|
let testValue = numbersArray[0];
|
||||||
|
if (testValue === undefined) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
let arrayLength = numbersArray.length;
|
||||||
|
let totalSum = 0;
|
||||||
|
for (i = 0; i < arrayLength; i++) {
|
||||||
|
totalSum = totalSum + numbersArray[i];
|
||||||
|
}
|
||||||
|
return totalSum;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function multiply () {
|
function multiply (numbersArray) {
|
||||||
|
let testValue = numbersArray[0];
|
||||||
|
if (testValue === undefined) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
let arrayLength = numbersArray.length;
|
||||||
|
console.log("array length : " + arrayLength);
|
||||||
|
let totalMultiply = 1;
|
||||||
|
console.log("early total sum : " + totalMultiply);
|
||||||
|
for (i = 0; i < arrayLength; i++) {
|
||||||
|
totalMultiply = totalMultiply * numbersArray[i];
|
||||||
|
console.log("loop " + i + " current " + totalMultiply);
|
||||||
|
}
|
||||||
|
return totalMultiply;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function power() {
|
function power(number, power) { // (2,4)
|
||||||
|
const number1 = number; //2
|
||||||
|
let power1 = power; //4
|
||||||
|
let total = number; //2
|
||||||
|
|
||||||
|
for (i = 0; i < power - 1; i++) {
|
||||||
|
//power-1 = 3
|
||||||
|
total = total * number1;
|
||||||
|
console.log(
|
||||||
|
"loop : " + i + " / number1 : " + number1 + " / total : " + total
|
||||||
|
);
|
||||||
|
// i=0, number1=2, total=4
|
||||||
|
// i=1, number1=2, total=8
|
||||||
|
// i=2, number1=2, total=8*2=16
|
||||||
|
// i=3, endloop total=16
|
||||||
|
}
|
||||||
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
function factorial() {
|
function factorial(factorialNo) {
|
||||||
|
let total = factorialNo;
|
||||||
|
let minusTotal = factorialNo - 1;
|
||||||
|
if (total === 0 || total === 1) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
for (i = total; i != 1; i--) {
|
||||||
|
total = total * minusTotal;
|
||||||
|
minusTotal--;
|
||||||
|
console.log(
|
||||||
|
"loop : " + i + "/ total : " + total + "/ minustotal : " + minusTotal
|
||||||
|
);
|
||||||
|
// i=3, total=3, total*minustotal=3*2, minustotal=1
|
||||||
|
// i=2, total=6, total*minustotal-6*1, minustotal=0
|
||||||
|
// i=1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -5,73 +5,73 @@ describe('add', function() {
|
||||||
expect(calculator.add(0,0)).toEqual(0);
|
expect(calculator.add(0,0)).toEqual(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
xit('adds 2 and 2', function() {
|
it('adds 2 and 2', function() {
|
||||||
expect(calculator.add(2,2)).toEqual(4);
|
expect(calculator.add(2,2)).toEqual(4);
|
||||||
});
|
});
|
||||||
|
|
||||||
xit('adds positive numbers', function() {
|
it('adds positive numbers', function() {
|
||||||
expect(calculator.add(2,6)).toEqual(8);
|
expect(calculator.add(2,6)).toEqual(8);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('subtract', function() {
|
describe('subtract', function() {
|
||||||
xit('subtracts numbers', function() {
|
it('subtracts numbers', function() {
|
||||||
expect(calculator.subtract(10,4)).toEqual(6);
|
expect(calculator.subtract(10,4)).toEqual(6);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('sum', function() {
|
describe('sum', function() {
|
||||||
xit('computes the sum of an empty array', function() {
|
it('computes the sum of an empty array', function() {
|
||||||
expect(calculator.sum([])).toEqual(0);
|
expect(calculator.sum([])).toEqual(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
xit('computes the sum of an array of one number', function() {
|
it('computes the sum of an array of one number', function() {
|
||||||
expect(calculator.sum([7])).toEqual(7);
|
expect(calculator.sum([7])).toEqual(7);
|
||||||
});
|
});
|
||||||
|
|
||||||
xit('computes the sum of an array of two numbers', function() {
|
it('computes the sum of an array of two numbers', function() {
|
||||||
expect(calculator.sum([7,11])).toEqual(18);
|
expect(calculator.sum([7,11])).toEqual(18);
|
||||||
});
|
});
|
||||||
|
|
||||||
xit('computes the sum of an array of many numbers', function() {
|
it('computes the sum of an array of many numbers', function() {
|
||||||
expect(calculator.sum([1,3,5,7,9])).toEqual(25);
|
expect(calculator.sum([1,3,5,7,9])).toEqual(25);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('multiply', function() {
|
describe('multiply', function() {
|
||||||
xit('multiplies two numbers', function() {
|
it('multiplies two numbers', function() {
|
||||||
expect(calculator.multiply([2,4])).toEqual(8);
|
expect(calculator.multiply([2,4])).toEqual(8);
|
||||||
});
|
});
|
||||||
|
|
||||||
xit('multiplies several numbers', function() {
|
it('multiplies several numbers', function() {
|
||||||
expect(calculator.multiply([2,4,6,8,10,12,14])).toEqual(645120);
|
expect(calculator.multiply([2,4,6,8,10,12,14])).toEqual(645120);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('power', function() {
|
describe('power', function() {
|
||||||
xit('raises one number to the power of another number', function() {
|
it('raises one number to the power of another number', function() {
|
||||||
expect(calculator.power(4,3)).toEqual(64); // 4 to third power is 64
|
expect(calculator.power(4,3)).toEqual(64); // 4 to third power is 64
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('factorial', function() {
|
describe('factorial', function() {
|
||||||
xit('computes the factorial of 0', function() {
|
it('computes the factorial of 0', function() {
|
||||||
expect(calculator.factorial(0)).toEqual(1); // 0! = 1
|
expect(calculator.factorial(0)).toEqual(1); // 0! = 1
|
||||||
});
|
});
|
||||||
|
|
||||||
xit('computes the factorial of 1', function() {
|
it('computes the factorial of 1', function() {
|
||||||
expect(calculator.factorial(1)).toEqual(1);
|
expect(calculator.factorial(1)).toEqual(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
xit('computes the factorial of 2', function() {
|
it('computes the factorial of 2', function() {
|
||||||
expect(calculator.factorial(2)).toEqual(2);
|
expect(calculator.factorial(2)).toEqual(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
xit('computes the factorial of 5', function() {
|
it('computes the factorial of 5', function() {
|
||||||
expect(calculator.factorial(5)).toEqual(120);
|
expect(calculator.factorial(5)).toEqual(120);
|
||||||
});
|
});
|
||||||
|
|
||||||
xit('computes the factorial of 10', function() {
|
it('computes the factorial of 10', function() {
|
||||||
expect(calculator.factorial(10)).toEqual(3628800);
|
expect(calculator.factorial(10)).toEqual(3628800);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
|
<title>Document</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
|
||||||
|
<script src="test.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -1,5 +1,39 @@
|
||||||
const palindromes = function() {
|
const palindromes = function(word) {
|
||||||
|
let array = [];
|
||||||
|
let readThis = word.toLowerCase();
|
||||||
|
|
||||||
|
for (i = 0; i < readThis.length; i++) {
|
||||||
|
if (
|
||||||
|
word.charAt(i) === "!" ||
|
||||||
|
word.charAt(i) === " " ||
|
||||||
|
word.charAt(i) === "," ||
|
||||||
|
word.charAt(i) === "."
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
array.push(readThis.charAt(i));
|
||||||
|
} // array should not have the ! character now e.g. from kecak! to [k,e,c,a,k]
|
||||||
|
|
||||||
|
let copyArray = [...array]; //ES6 style copy
|
||||||
|
let newArray = [];
|
||||||
|
|
||||||
|
while (copyArray.length != 0) {
|
||||||
|
newArray.push(copyArray.pop());
|
||||||
|
} // should be [k,a,c,e,k]
|
||||||
|
|
||||||
|
console.log("array: " + array);
|
||||||
|
console.log("newArray: " + newArray);
|
||||||
|
|
||||||
|
let word1 = array.toString();
|
||||||
|
let word2 = newArray.toString();
|
||||||
|
|
||||||
|
if (word1 == word2) {
|
||||||
|
// console.log("it's a palindrome!");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
// console.log("it's NOT a palindrome!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = palindromes
|
module.exports = palindromes
|
||||||
|
|
|
@ -4,16 +4,16 @@ describe('palindromes', function() {
|
||||||
it('works with single words', function() {
|
it('works with single words', function() {
|
||||||
expect(palindromes('racecar')).toEqual(true);
|
expect(palindromes('racecar')).toEqual(true);
|
||||||
});
|
});
|
||||||
xit('works with punctuation', function() {
|
it('works with punctuation', function() {
|
||||||
expect(palindromes('Racecar!')).toEqual(true);
|
expect(palindromes('Racecar!')).toEqual(true);
|
||||||
});
|
});
|
||||||
xit('works with multiple words', function() {
|
it('works with multiple words', function() {
|
||||||
expect(palindromes('A car, a man, a maraca.')).toEqual(true);
|
expect(palindromes('A car, a man, a maraca.')).toEqual(true);
|
||||||
});
|
});
|
||||||
xit('works with multiple words', function() {
|
it('works with multiple words', function() {
|
||||||
expect(palindromes('Animal loots foliated detail of stool lamina.')).toEqual(true);
|
expect(palindromes('Animal loots foliated detail of stool lamina.')).toEqual(true);
|
||||||
});
|
});
|
||||||
xit('doesn\'t just always return true', function() {
|
it('doesn\'t just always return true', function() {
|
||||||
expect(palindromes('ZZZZ car, a man, a maraca.')).toEqual(false);
|
expect(palindromes('ZZZZ car, a man, a maraca.')).toEqual(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
function palindrome(word) {
|
||||||
|
let array = [];
|
||||||
|
let readThis = word.toLowerCase();
|
||||||
|
|
||||||
|
for (i = 0; i < readThis.length; i++) {
|
||||||
|
if (word.charAt(i) === "!" || word.charAt(i) === " " || word.charAt(i) === "," || word.charAt(i) === '.') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
array.push(readThis.charAt(i));
|
||||||
|
} // array should not have the ! character now e.g. from kecak! to [k,e,c,a,k]
|
||||||
|
|
||||||
|
let copyArray = [...array]; //ES6 style copy
|
||||||
|
let newArray = [];
|
||||||
|
|
||||||
|
while (copyArray.length != 0) {
|
||||||
|
newArray.push(copyArray.pop());
|
||||||
|
} // should be [k,a,c,e,k]
|
||||||
|
|
||||||
|
console.log("array: " + array);
|
||||||
|
console.log("newArray: " + newArray);
|
||||||
|
|
||||||
|
let word1 = array.toString();
|
||||||
|
let word2 = newArray.toString();
|
||||||
|
|
||||||
|
if (word1 == word2) {
|
||||||
|
console.log("it's a palindrome!");
|
||||||
|
} else {
|
||||||
|
console.log("it's NOT a palindrome!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
palindrome("ZZZZ car, a man, a maraca.");
|
Loading…
Reference in New Issue