Can someone assist me with a situation where I need to replace the last quotation mark in a string if there is an odd number of quotation marks? I tried implementing a solution but it doesn't seem to work. Here's my code:
const input = `"hello,"sai,sur",ya,teja`;
let output = "";
if(evenOrOdd(input.split(`"`) == "even")){
//Here the last occurrence which needed to be replaced with empty string
input[input.split(`"`).lastIndexOf(`"`)] = "";
console.log(input);
output = input.replace(/"([^"]+)"/g, (_, g) => g.replace(',', '-'))
}else{
output = input.replace(/"([^"]+)"/g, (_, g) => g.replace(',', '-'))
}
console.log(output);
function evenOrOdd(number){
//check if the number is even
if(number % 2 == 0) {
console.log("The number is even.");
return "even";
}
// if the number is odd
else {
console.log("The number is odd.");
return "odd";
}
}
Thanks in advance :)