I am trying to teach myself about Ternary Operators, and am stuck on a problem. To best explain what I am trying to do, below is pseudocode of what I want my code to look like:
const regex = /\d+k\d+/;
const input = "8k4";
const response = (!regex.test(input) ? "Regex does not match." : (
const roll = input.substring(0);
const keep = input.substring(2);
(parseInt(roll) >= parseInt(keep) ? "Correct Format!" : "Keep is greater than Roll." )
);
console.log(response);
In essence, I am trying to replicate something like the following if/else code but using ternary operators (in order to condense my code), and I cannot seem to find the correct format for declaring the const bits in the second condition of the ternary operation:
const response = function() {
if(!regex.test(input)) {
return "Regex does not match.";
} else {
const roll = input.substring(0);
const keep = input.substring(2);
if(parseInt(roll) >= parseInt(keep)) {
return "Correct Format!";
} else {
return "Keep is greater than Roll."
}
}
}();
For context, I am building a dice-rolling Discord Bot using discord.js so my friends and I don't need to be together to play tabletop games in the same place, hence the "roll" and "keep" variables.
substring(2)doesn't work on e.g.12k20