Skip to content

Commit 7705980

Browse files
committed
adding new functions
1 parent 15c6821 commit 7705980

File tree

6 files changed

+223
-1
lines changed

6 files changed

+223
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* displays if number is happy number or not
3+
*
4+
* @param {number} n
5+
* @returns {boolean}
6+
*/
7+
export default function isHappyNumber(n: number): boolean {
8+
var map: any = {};
9+
var tmp = 0;
10+
11+
if (n < 1) return false;
12+
13+
while (n !== 1 && !map[n]) {
14+
map[n] = true;
15+
tmp = 0;
16+
17+
while (n > 0) {
18+
tmp += Math.pow(n % 10, 2);
19+
n = Math.floor(n / 10);
20+
}
21+
22+
n = tmp;
23+
}
24+
25+
return n === 1;
26+
}

project/ts/functionality/main.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import removeDuplicate from "./removeDuplicate";
1717
import reverseString from "./reverseString";
1818
import reverseNum from "./reverseNum";
1919
import shuffle from "./shuffle";
20+
import randomString from "./randomString";
21+
import randomColor from "./randomColor";
22+
import randomHsl from "./randomHsl";
23+
import isHappyNumber from "./isHappyNumber";
2024

2125
const functionality = {
2226
sumOfArray,
@@ -38,6 +42,10 @@ const functionality = {
3842
reverseString,
3943
reverseNum,
4044
shuffle,
45+
randomString,
46+
randomColor,
47+
randomHsl,
48+
isHappyNumber,
4149
};
4250

4351
declare global {
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/**
2+
* displays a random color
3+
*
4+
* @returns {string}
5+
*/
6+
export default function randomColor(): string {
7+
const CSS_COLOR_NAMES = [
8+
"AliceBlue",
9+
"AntiqueWhite",
10+
"Aqua",
11+
"Aquamarine",
12+
"Azure",
13+
"Beige",
14+
"Bisque",
15+
"Black",
16+
"BlanchedAlmond",
17+
"Blue",
18+
"BlueViolet",
19+
"Brown",
20+
"BurlyWood",
21+
"CadetBlue",
22+
"Chartreuse",
23+
"Chocolate",
24+
"Coral",
25+
"CornflowerBlue",
26+
"Cornsilk",
27+
"Crimson",
28+
"Cyan",
29+
"DarkBlue",
30+
"DarkCyan",
31+
"DarkGoldenRod",
32+
"DarkGray",
33+
"DarkGrey",
34+
"DarkGreen",
35+
"DarkKhaki",
36+
"DarkMagenta",
37+
"DarkOliveGreen",
38+
"DarkOrange",
39+
"DarkOrchid",
40+
"DarkRed",
41+
"DarkSalmon",
42+
"DarkSeaGreen",
43+
"DarkSlateBlue",
44+
"DarkSlateGray",
45+
"DarkSlateGrey",
46+
"DarkTurquoise",
47+
"DarkViolet",
48+
"DeepPink",
49+
"DeepSkyBlue",
50+
"DimGray",
51+
"DimGrey",
52+
"DodgerBlue",
53+
"FireBrick",
54+
"FloralWhite",
55+
"ForestGreen",
56+
"Fuchsia",
57+
"Gainsboro",
58+
"GhostWhite",
59+
"Gold",
60+
"GoldenRod",
61+
"Gray",
62+
"Grey",
63+
"Green",
64+
"GreenYellow",
65+
"HoneyDew",
66+
"HotPink",
67+
"IndianRed",
68+
"Indigo",
69+
"Ivory",
70+
"Khaki",
71+
"Lavender",
72+
"LavenderBlush",
73+
"LawnGreen",
74+
"LemonChiffon",
75+
"LightBlue",
76+
"LightCoral",
77+
"LightCyan",
78+
"LightGoldenRodYellow",
79+
"LightGray",
80+
"LightGrey",
81+
"LightGreen",
82+
"LightPink",
83+
"LightSalmon",
84+
"LightSeaGreen",
85+
"LightSkyBlue",
86+
"LightSlateGray",
87+
"LightSlateGrey",
88+
"LightSteelBlue",
89+
"LightYellow",
90+
"Lime",
91+
"LimeGreen",
92+
"Linen",
93+
"Magenta",
94+
"Maroon",
95+
"MediumAquaMarine",
96+
"MediumBlue",
97+
"MediumOrchid",
98+
"MediumPurple",
99+
"MediumSeaGreen",
100+
"MediumSlateBlue",
101+
"MediumSpringGreen",
102+
"MediumTurquoise",
103+
"MediumVioletRed",
104+
"MidnightBlue",
105+
"MintCream",
106+
"MistyRose",
107+
"Moccasin",
108+
"NavajoWhite",
109+
"Navy",
110+
"OldLace",
111+
"Olive",
112+
"OliveDrab",
113+
"Orange",
114+
"OrangeRed",
115+
"Orchid",
116+
"PaleGoldenRod",
117+
"PaleGreen",
118+
"PaleTurquoise",
119+
"PaleVioletRed",
120+
"PapayaWhip",
121+
"PeachPuff",
122+
"Peru",
123+
"Pink",
124+
"Plum",
125+
"PowderBlue",
126+
"Purple",
127+
"RebeccaPurple",
128+
"Red",
129+
"RosyBrown",
130+
"RoyalBlue",
131+
"SaddleBrown",
132+
"Salmon",
133+
"SandyBrown",
134+
"SeaGreen",
135+
"SeaShell",
136+
"Sienna",
137+
"Silver",
138+
"SkyBlue",
139+
"SlateBlue",
140+
"SlateGray",
141+
"SlateGrey",
142+
"Snow",
143+
"SpringGreen",
144+
"SteelBlue",
145+
"Tan",
146+
"Teal",
147+
"Thistle",
148+
"Tomato",
149+
"Turquoise",
150+
"Violet",
151+
"Wheat",
152+
"White",
153+
"WhiteSmoke",
154+
"Yellow",
155+
"YellowGreen",
156+
];
157+
return CSS_COLOR_NAMES[Math.floor(Math.random() * CSS_COLOR_NAMES.length)];
158+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* displays a random hsl color
3+
*
4+
* @returns {string}
5+
*/
6+
export default function randomHsl(): string {
7+
const h = Math.floor(Math.random() * 360);
8+
const s = Math.floor(Math.random() * 100);
9+
const l = Math.floor(Math.random() * 100);
10+
return `hsl(${h}, ${s}%, ${l}%)`;
11+
}

project/ts/functionality/randomNum.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Get a random number
2+
* Generate a random number
33
*
44
* @param {number} start
55
* @param {number} end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Generate a random String
3+
*
4+
* @param {number} length
5+
* @returns {string}
6+
*/
7+
export default function randomString(length: number = 5): string {
8+
if (typeof length !== "number") {
9+
throw new TypeError("Expected numbers but got " + typeof length);
10+
}
11+
var result = "";
12+
var characters =
13+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
14+
var charactersLength = characters.length;
15+
for (var i = 0; i < length; i++) {
16+
result += characters.charAt(Math.floor(Math.random() * charactersLength));
17+
}
18+
return result;
19+
}

0 commit comments

Comments
 (0)