Skip to content

Commit 1f214f9

Browse files
committed
JavaScript Algorithms and Data Structures Projects (ex. 1 to 5)
+ Finished JavaScript Algorithms and Data Structures Projects module
1 parent e3c41df commit 1f214f9

File tree

7 files changed

+359
-6
lines changed

7 files changed

+359
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Palindrome Checker:
3+
Return true if the given string is a palindrome. Otherwise, return false.
4+
A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case,
5+
and spacing.
6+
7+
Note: You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything into
8+
the same case (lower or upper case) in order to check for palindromes.
9+
10+
We'll pass strings with varying formats, such as racecar, RaceCar, and race CAR among others.
11+
We'll also pass strings with special symbols, such as 2A3*3a2, 2A3 3a2, and 2_A3*3#A2.
12+
13+
- palindrome("eye") should return a boolean.
14+
- palindrome("eye") should return true.
15+
- palindrome("_eye") should return true.
16+
- palindrome("race car") should return true.
17+
- palindrome("not a palindrome") should return false.
18+
- palindrome("A man, a plan, a canal. Panama") should return true.
19+
- palindrome("never odd or even") should return true.
20+
- palindrome("nope") should return false.
21+
- palindrome("almostomla") should return false.
22+
- palindrome("My age is 0, 0 si ega ym.") should return true.
23+
- palindrome("1 eye for of 1 eye.") should return false.
24+
- palindrome("0_0 (: /-\ :) 0-0") should return true.
25+
- palindrome("five|\_/|four") should return false.
26+
*/
27+
function palindrome(str) {
28+
let cleanString = str.replace(/[^0-9a-z]/gi, '').toLowerCase();
29+
const lastIndex = cleanString.length - 1;
30+
for (let j = 0; j < cleanString.length; j++) {
31+
if (cleanString[j] !== cleanString[lastIndex - j]) {
32+
return false;
33+
}
34+
}
35+
return true;
36+
}
37+
38+
console.log("expects: true", palindrome("eye"));
39+
console.log("expects: true", palindrome("_eye"));
40+
console.log("expects: true", palindrome("race car"));
41+
console.log("expects: false", palindrome("not a palindrome"));
42+
console.log("expects: true", palindrome("A man, a plan, a canal. Panama"));
43+
console.log("expects: true", palindrome("never odd or even"));
44+
console.log("expects: false", palindrome("nope"));
45+
console.log("expects: false", palindrome("almostomla"));
46+
console.log("expects: true", palindrome("My age is 0, 0 si ega ym."));
47+
console.log("expects: false", palindrome("1 eye for of 1 eye."));
48+
console.log("expects: true", palindrome("0_0 (: /-\ :) 0-0"));
49+
console.log("expects: false", palindrome("five|\_/|four"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Roman Numeral Converter
3+
Convert the given number into a roman numeral.
4+
5+
All roman numerals (https://www.mathsisfun.com/roman-numerals.html) answers should be provided in upper-case.
6+
7+
- convertToRoman(2) should return the string II.
8+
- convertToRoman(3) should return the string III.
9+
- convertToRoman(4) should return the string IV.
10+
- convertToRoman(5) should return the string V.
11+
- convertToRoman(9) should return the string IX.
12+
- convertToRoman(12) should return the string XII.
13+
- convertToRoman(16) should return the string XVI.
14+
- convertToRoman(29) should return the string XXIX.
15+
- convertToRoman(44) should return the string XLIV.
16+
- convertToRoman(45) should return the string XLV.
17+
- convertToRoman(68) should return the string LXVIII
18+
- convertToRoman(83) should return the string LXXXIII
19+
- convertToRoman(97) should return the string XCVII
20+
- convertToRoman(99) should return the string XCIX
21+
- convertToRoman(400) should return the string CD
22+
- convertToRoman(500) should return the string D
23+
- convertToRoman(501) should return the string DI
24+
- convertToRoman(649) should return the string DCXLIX
25+
- convertToRoman(798) should return the string DCCXCVIII
26+
- convertToRoman(891) should return the string DCCCXCI
27+
- convertToRoman(1000) should return the string M
28+
- convertToRoman(1004) should return the string MIV
29+
- convertToRoman(1006) should return the string MVI
30+
- convertToRoman(1023) should return the string MXXIII
31+
- convertToRoman(2014) should return the string MMXIV
32+
- convertToRoman(3999) should return the string MMMCMXCIX
33+
*/
34+
function convertToRoman(num) {
35+
const romanToNumMap = {
36+
M: 1000,
37+
CM: 900,
38+
D: 500,
39+
CD: 400,
40+
C: 100,
41+
XC: 90,
42+
L: 50,
43+
XL: 40,
44+
X: 10,
45+
IX: 9,
46+
V: 5,
47+
IV: 4,
48+
I: 1
49+
}
50+
let romanFromNum = "";
51+
for (const key in romanToNumMap) {
52+
const mapNum = romanToNumMap[key];
53+
54+
while (num >= mapNum) {
55+
romanFromNum += key;
56+
num -= mapNum;
57+
}
58+
}
59+
60+
return romanFromNum;
61+
}
62+
63+
console.log(convertToRoman(36));
64+
console.log(convertToRoman(2));
65+
console.log(convertToRoman(3));
66+
console.log(convertToRoman(4));
67+
console.log(convertToRoman(5));
68+
console.log(convertToRoman(9));
69+
console.log(convertToRoman(12));
70+
console.log(convertToRoman(16));
71+
console.log(convertToRoman(29));
72+
console.log(convertToRoman(44));
73+
console.log(convertToRoman(45));
74+
console.log(convertToRoman(68));
75+
console.log(convertToRoman(83));
76+
console.log(convertToRoman(97));
77+
console.log(convertToRoman(99));
78+
console.log(convertToRoman(400));
79+
console.log(convertToRoman(500));
80+
console.log(convertToRoman(501));
81+
console.log(convertToRoman(649));
82+
console.log(convertToRoman(798));
83+
console.log(convertToRoman(891));
84+
console.log(convertToRoman(1000));
85+
console.log(convertToRoman(1004));
86+
console.log(convertToRoman(1006));
87+
console.log(convertToRoman(1023));
88+
console.log(convertToRoman(2014));
89+
console.log(convertToRoman(3999));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Caesars Cipher:
3+
One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher.
4+
In a shift cipher the meanings of the letters are shifted by some set amount.
5+
A common modern use is the ROT13 cipher (https://en.wikipedia.org/wiki/ROT13), where the values of the letters are
6+
shifted by 13 places. Thus A ↔ N, B ↔ O and so on.
7+
8+
Write a function which takes a ROT13 encoded string as input and returns a decoded string.
9+
10+
All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass
11+
them on.
12+
13+
- rot13("SERR PBQR PNZC") should decode to the string FREE CODE CAMP
14+
- rot13("SERR CVMMN!") should decode to the string FREE PIZZA!
15+
- rot13("SERR YBIR?") should decode to the string FREE LOVE?
16+
- rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.") should decode to the string THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
17+
*/
18+
function rot13(str) {
19+
const shiftCharacters = 13;
20+
const alphabetCharCodeMiddleIndex = 'A'.charCodeAt(0) + shiftCharacters;
21+
return str.replaceAll(/[A-Z]/gi,
22+
word => word.charCodeAt(0) < alphabetCharCodeMiddleIndex
23+
? String.fromCharCode(word.charCodeAt(0) + shiftCharacters)
24+
: String.fromCharCode(word.charCodeAt(0) - shiftCharacters));
25+
}
26+
27+
console.log(rot13("SERR PBQR PNZC"));
28+
console.log(rot13("SERR CVMMN!"));
29+
console.log(rot13("SERR YBIR?"));
30+
console.log(rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT."));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Telephone Number Validator:
3+
Return true if the passed string looks like a valid US phone number.
4+
5+
The user may fill out the form field any way they choose as long as it has the format of a valid US number. The following are examples of valid formats for US numbers (refer to the tests below for other variants):
6+
7+
555-555-5555
8+
(555)555-5555
9+
(555) 555-5555
10+
555 555 5555
11+
5555555555
12+
1 555 555 5555
13+
14+
For this challenge you will be presented with a string such as 800-692-7753 or 8oo-six427676;laskdjf.
15+
Your job is to validate or reject the US phone number based on any combination of the formats provided above.
16+
The area code is required. If the country code is provided, you must confirm that the country code is 1.
17+
Return true if the string is a valid US phone number; otherwise return false.
18+
19+
- telephoneCheck("555-555-5555") should return a boolean.
20+
- telephoneCheck("1 555-555-5555") should return true.
21+
- telephoneCheck("1 (555) 555-5555") should return true.
22+
- telephoneCheck("5555555555") should return true.
23+
- telephoneCheck("555-555-5555") should return true.
24+
- telephoneCheck("(555)555-5555") should return true.
25+
- telephoneCheck("1(555)555-5555") should return true.
26+
- telephoneCheck("555-5555") should return false.
27+
- telephoneCheck("5555555") should return false.
28+
- telephoneCheck("1 555)555-5555") should return false.
29+
- telephoneCheck("1 555 555 5555") should return true.
30+
- telephoneCheck("1 456 789 4444") should return true.
31+
- telephoneCheck("123**&!!asdf#") should return false.
32+
- telephoneCheck("55555555") should return false.
33+
- telephoneCheck("(6054756961)") should return false.
34+
- telephoneCheck("2 (757) 622-7382") should return false.
35+
- telephoneCheck("0 (757) 622-7382") should return false.
36+
- telephoneCheck("-1 (757) 622-7382") should return false.
37+
- telephoneCheck("2 757 622-7382") should return false.
38+
- telephoneCheck("10 (757) 622-7382") should return false.
39+
- telephoneCheck("27576227382") should return false.
40+
- telephoneCheck("(275)76227382") should return false.
41+
- telephoneCheck("2(757)6227382") should return false.
42+
- telephoneCheck("2(757)622-7382") should return false.
43+
- telephoneCheck("555)-555-5555") should return false.
44+
- telephoneCheck("(555-555-5555") should return false.
45+
- telephoneCheck("(555)5(55?)-5555") should return false.
46+
- telephoneCheck("55 55-55-555-5") should return false.
47+
*/
48+
function telephoneCheck(str) {
49+
const regExp = /^1?\s?(\(\d{3}\)|\d{3})(\s|-)?\d{3}(\s|-)?\d{4}$/gm;
50+
return regExp.test(str);
51+
}
52+
53+
console.log(telephoneCheck("555-555-5555"));
54+
console.log(telephoneCheck("1 555-555-5555"));
55+
console.log(telephoneCheck("1 (555) 555-5555"));
56+
console.log(telephoneCheck("5555555555"));
57+
console.log(telephoneCheck("555-555-5555"));
58+
console.log(telephoneCheck("(555)555-5555"));
59+
console.log(telephoneCheck("1(555)555-5555"));
60+
console.log(telephoneCheck("555-5555"));
61+
console.log(telephoneCheck("5555555"));
62+
console.log(telephoneCheck("1 555)555-5555"));
63+
console.log(telephoneCheck("1 555 555 5555"));
64+
console.log(telephoneCheck("1 456 789 4444"));
65+
console.log(telephoneCheck("123**&!!asdf#"));
66+
console.log(telephoneCheck("55555555"));
67+
console.log(telephoneCheck("(6054756961)"));
68+
console.log(telephoneCheck("2 (757) 622-7382"));
69+
console.log(telephoneCheck("0 (757) 622-7382"));
70+
console.log(telephoneCheck("-1 (757) 622-7382"));
71+
console.log(telephoneCheck("2 757 622-7382"));
72+
console.log(telephoneCheck("10 (757) 622-7382"));
73+
console.log(telephoneCheck("27576227382"));
74+
console.log(telephoneCheck("(275)76227382"));
75+
console.log(telephoneCheck("2(757)6227382"));
76+
console.log(telephoneCheck("2(757)622-7382"));
77+
console.log(telephoneCheck("555)-555-5555"));
78+
console.log(telephoneCheck("(555-555-5555"));
79+
console.log(telephoneCheck("(555)5(55?)-5555"));
80+
console.log(telephoneCheck("55 55-55-555-5"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Cash Register:
3+
Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price),
4+
payment as the second argument (cash), and cash-in-drawer (cid) as the third argument.
5+
6+
cid is a 2D array listing available currency.
7+
8+
The checkCashRegister() function should always return an object with a status key and a change key.
9+
10+
Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or if you cannot return the exact change.
11+
12+
Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.
13+
14+
Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order, as the value of the change key.
15+
16+
Currency Unit Amount
17+
Penny $0.01 (PENNY)
18+
Nickel $0.05 (NICKEL)
19+
Dime $0.1 (DIME)
20+
Quarter $0.25 (QUARTER)
21+
Dollar $1 (ONE)
22+
Five Dollars $5 (FIVE)
23+
Ten Dollars $10 (TEN)
24+
Twenty Dollars $20 (TWENTY)
25+
One-hundred Dollars $100 (ONE HUNDRED)
26+
See below for an example of a cash-in-drawer array:
27+
28+
[
29+
["PENNY", 1.01],
30+
["NICKEL", 2.05],
31+
["DIME", 3.1],
32+
["QUARTER", 4.25],
33+
["ONE", 90],
34+
["FIVE", 55],
35+
["TEN", 20],
36+
["TWENTY", 60],
37+
["ONE HUNDRED", 100]
38+
]
39+
40+
- checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]) should return an object.
41+
- checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]) should return {status: "OPEN", change: [["QUARTER", 0.5]]}.
42+
- checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]) should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}.
43+
- checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []}.
44+
- checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []}.
45+
- checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}.
46+
*/
47+
const MONEY_MAP = [
48+
{name: "ONE HUNDRED", value: 100.0},
49+
{name: "TWENTY", value: 20.0},
50+
{name: "TEN", value: 10.0},
51+
{name: "FIVE", value: 5.0},
52+
{name: "ONE", value: 1.0},
53+
{name: "QUARTER", value: 0.25},
54+
{name: "DIME", value: 0.1},
55+
{name: "NICKEL", value: 0.05},
56+
{name: "PENNY", value: 0.01},
57+
];
58+
59+
function checkCashRegister(price, cash, cid) {
60+
const result = {status: "", change: [],};
61+
let change = cash - price;
62+
const moneyInStoreSum = cid
63+
.map(moneyType => moneyType[1])
64+
.reduce((sum, value) => sum + value);
65+
66+
if (change > moneyInStoreSum) {
67+
result.status = "INSUFFICIENT_FUNDS";
68+
69+
} else if (change === moneyInStoreSum) {
70+
result.status = "CLOSED";
71+
result.change = cid;
72+
73+
} else {
74+
let changeAmounts = [];
75+
let sortedCid = cid.reverse();
76+
77+
for (let i = 0; i < sortedCid.length; i++) {
78+
let amountPaidPerType = 0;
79+
80+
while (MONEY_MAP[i].value <= change && sortedCid[i][1] > 0) {
81+
sortedCid[i][1] -= MONEY_MAP[i].value;
82+
change = change.toFixed(2);
83+
change -= MONEY_MAP[i].value;
84+
amountPaidPerType += MONEY_MAP[i].value;
85+
}
86+
87+
if (amountPaidPerType > 0) {
88+
changeAmounts.push([sortedCid[i][0], amountPaidPerType]);
89+
}
90+
}
91+
92+
if (change > 0) {
93+
result.status = "INSUFFICIENT_FUNDS";
94+
} else {
95+
result.status = "OPEN";
96+
result.change = changeAmounts;
97+
}
98+
}
99+
100+
return result;
101+
}
102+
103+
console.log(checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));
104+
console.log(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));
105+
console.log(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));

Diff for: 10-javascript-algorithms-and-data-structures-projects/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ freeCodeCamp module description:
99
1010
### Exercises
1111

12-
- [ ] [ 1 - Palindrome Checker]()
13-
- [ ] [ 2 - Roman Numeral Converter]()
14-
- [ ] [ 3 - Caesars Cipher]()
15-
- [ ] [ 4 - Telephone Number Validator]()
16-
- [ ] [ 5 - Cash Register]()
12+
- [X] [ 1 - Palindrome Checker](1-palindrome-checker.js)
13+
- [X] [ 2 - Roman Numeral Converter](2-roman-numeral-converter.js)
14+
- [X] [ 3 - Caesars Cipher](3-caesars-cipher.js)
15+
- [X] [ 4 - Telephone Number Validator](4-telephone-number-validator.js)
16+
- [X] [ 5 - Cash Register](5-cash-register.js)
1717

1818
⬅️ [Back to main file](../README.md)
1919

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ freeCodeCamp course description:
1818
- [X] [ 07 - Object Oriented Programming](/07-object-oriented-programming/README.md)
1919
- [X] [ 08 - Functional Programming](/08-functional-programming/README.md)
2020
- [X] [ 09 - Intermediate Algorithm Scripting](/09-intermediate-algorithm-scripting/README.md)
21-
- [ ] [ 10 - JavaScript Algorithms and Data Structures Projects](/10-javascript-algorithms-and-data-structures-projects/README.md)
21+
- [X] [ 10 - JavaScript Algorithms and Data Structures Projects](/10-javascript-algorithms-and-data-structures-projects/README.md)
2222

2323
### Languages
2424

0 commit comments

Comments
 (0)