Skip to content

Commit cd3b9c7

Browse files
Merge pull request matthewsamuel95#891 from 2alin/add-last_digit_exp-JS
Added last digit of exp javascript
2 parents fb90de5 + 30af07d commit cd3b9c7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Calculates the last digit of a^b
3+
* Args:
4+
* a (int): Base of a^b
5+
* b (int): Exponent of a^b
6+
* Returns:
7+
* (int): The last digit of a^b
8+
*/
9+
let last_digit_of = (a,b) => {
10+
let last_digit_a = parseInt(String(a).slice(-1));
11+
let exp;
12+
13+
if (b % 4 === 0) {
14+
exp = 4;
15+
} else {
16+
exp = b % 4;
17+
}
18+
19+
return (Math.pow(last_digit_a, exp) % 10)
20+
};
21+
22+
23+
/*
24+
* Test Cases
25+
*/
26+
let tests = [[3,10], [6,2], [150,530]]
27+
console.log("Tests:")
28+
for (let array of tests) {
29+
console.log("last digit of " + array[0] + "^" + array [1] + " is "
30+
+ last_digit_of(array[0], array[1]));
31+
}

0 commit comments

Comments
 (0)