I'm using an api to get some product details that display on my WordPress page (total beginner in WordPress & JavaScript).
- Response from api: 1234
- What i need: 12.34€
- Sometimes i get no data back --> no price available
What i came up with so far:
$(".offer-price, .-list-price").text(function(index, text) {
if (text.length == 0) {
return ""
} else {
var digits = text.split("");
var back = digits.slice(-2).join("");
var front = digits.slice(0,-2).join("");
return front + "." + back + "\u20AC";
}
});
This function solves my problem. Since i am trying to improve / learn i would love to get some feedback from you guys on my approach and alternative ways to solve the issue. I'm sure there are better ways to tackle this?!