-
Notifications
You must be signed in to change notification settings - Fork 130
Create Binary To Decimal Converter.js #456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| activation_example:!converttoDecimal <binary_code> | ||
| regex:^!converttoDecimal | ||
| flags:i | ||
| */ | ||
|
|
||
| // get the user input in format !converttoDecimal binary_code for example - !converttoDecimal 1011 | ||
| var input = current.text.trim(); | ||
|
|
||
|
|
||
| // Extract the Binary expression from the input | ||
| var binaryCheckregx = /^[01]+$/; // regular expresion for valid binary code | ||
| var match = input.match(/!converttoDecimal (\d+)/); // check input format | ||
| var expression = match ? match[1] : ""; | ||
|
||
|
|
||
|
|
||
|
|
||
| function evaluateBinaryExpression(binary_code) { | ||
| binary_code = binary_code.replace(/\s+/g, ''); // Remove whitespace | ||
| // execute the api to get the equivalent decimal value of binary | ||
| var converter = new sn_ws.RESTMessageV2(); | ||
| var apiurl = 'https://networkcalc.com/api/binary/' + binary_code; | ||
| converter.setEndpoint(apiurl); // set endpoint url | ||
| converter.setHttpMethod('GET'); // GET method | ||
| var response = converter.execute(); | ||
| var result = JSON.parse(response.getBody()); // parse the response object | ||
| return result; | ||
| } | ||
|
|
||
| try{ | ||
| // check the valid binary code in if statement | ||
| if(binaryCheckregx.test(expression)){ | ||
| var result = evaluateBinaryExpression(expression); // call to evaluateBinaryExpression | ||
| var DecimalresultSlackMessage = "* Binary to Decimal Conversion :*\n" + "• *Binary value*: " + expression + "\n" + "• *Decimal value*: " + result.converted + "\n"; // slack markdown message format | ||
|
||
| new x_snc_slackerbot.Slacker().send_chat(current, DecimalresultSlackMessage , false); // display the output to user | ||
| } | ||
| else{ | ||
| new x_snc_slackerbot.Slacker().send_chat(current, "Oops! I couldn't understand that. Please provide a valid Binary code.", false); | ||
| } | ||
| } | ||
| catch(error){ | ||
| new x_snc_slackerbot.Slacker().send_chat(current, "Oops! Facing Issue while converting Binary to Decimal with error" + error, false); //handling exception in try block | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this capitalisation, as this adds confusion when compared to all other parsers. Additionally, it is not clear that this parser converts from binary to decimal. It might be worthwhile making it something like
!bin2decThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I wil make changes