Day 17: More Exceptions

Day 17: More Exceptions

Objective

Yesterday’s challenge taught you to manage exceptional situations by using try and catch blocks. In today’s challenge, you’re going to practice throwing and propagating an exception. Check out the Tutorial tab for learning materials and an instructional video!

Task

Write a Calculator class with a single method: int power(int,int). The power method takes two integers, n and p, as parameters and returns the integer result of np. If either or is negative, then the method must throw an exception with the message: n and p should be non-negative.

Note: Do not use an access modifier (e.g.: public) in the declaration for your Calculator class.

Input Format

Input from stdin is handled for you by the locked stub code in your editor. The first line contains an integer, T, the number of test cases. Each of the T subsequent lines describes a test case in 2 space-separated integers denoting n and p, respectively.

Constraints

  • No Test Case will result in overflow for correctly written code.

Output Format

Output to stdout is handled for you by the locked stub code in your editor. There are T lines of output, where each line contains the result of np as calculated by your Calculator class’ power method.

Sample Input

1
2
3
4
5
4
3 5
2 4
-1 -2
-1 3

Sample Output

1
2
3
4
243
16
n and p should be non-negative
n and p should be non-negative

Explanation

T = 4
T0: 3 and 5 are positive, so power returns the result of 35, which is 243.

T1: 2 and 4 are positive, so power returns the result of 24=, which is 16.

T2: Both inputs (-1 and -2) are negative, so power throws an exception and n and p should be non-negative is printed.

T3: One of the inputs (-1) is negative, so power throws an exception and n and p should be non-negative is printed.




Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
* You can use the following pseudocode:
*
* `S` = read string from stdin.
* try {
* `N` = numeric value of `S`.
*
* If `N` is `NaN` or `undefined`, then some exception must be thrown,
* you can write the following withtout using conditional statement:
* <condition to return true when `N` is `NaN` or `undefined`> && an_undefined_function_call()
*
* print `N`
* } catch (err) {
* print `Bad String`.
* }
*/

//Write your code here
let Calculator = function () {
this.power = function (n, p) {
try {
if (n >= 0 && p >= 0) {
return n ** p;
} else {
throw 'n and p should be non-negative';
}
} catch (error) {
return error;
}
}
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×