Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Parsers/Binary To Decimal Converter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
activation_example:!converttoDecimal <binary_code>
regex:^!converttoDecimal
Copy link
Contributor

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 !bin2dec

Copy link
Contributor Author

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

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] : "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear from other details about this parser, that this will not support multiple bytes (i.e. 00110101 11111111) or breaks in the binary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I will rectify the code




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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not use unordered lists so does not use slack markdown

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

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
}