0

I am new to Javascript, so my apologies if this is a dumb question. I've read a number of threads on here and other sites looking for an answer, and yet to find a solution. I have borrowed part of the code below from another post here.

I use jquery to count some things on my web page so I can display the numbers. I also now want to add those numbers in a bit of JSON FAQ schema for Google to use. How can I get the variables to output properly in the below example? I can get them to output to the html using .text but now need that value in the schema as well.

Thank you

var dealcount = jQuery('article.deal').length;
var codecount = jQuery('article.code').length;
var printcount = jQuery('article.print').length;
var totalcount = jQuery('article.card').length;
var d = new Date();
d.setDate(d.getDate() - 1);
var n = d.toDateString().split(' ').slice(1).join(' ');

var schema = {
  "@context": "http://schema.org",
  "@type": "FAQPage",
  "mainEntity": [{
    "@type": "Question",
    "name": "How many things are there?",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "Currently, there are (DEALCOUNT) different offers including (CODECOUNT) codes, and (PRINTCOUNT) printables as of (n)"
    }
  }]
}
var script = document.createElement('script');
script.type = "application/ld+json";
script.text = JSON.stringify(schema);

document.querySelector('body').appendChild(script);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
itsmrchris
  • 13
  • 4
  • You need to use string concatenation to join the values: `'Currently, there are (' + dealcount + ') different offers ...'` https://flaviocopes.com/how-to-join-strings-javascript/ – Rory McCrossan Dec 06 '19 at 13:32

1 Answers1

1

You can use template strings:

      "text": `Currently, there are ${dealcount} different offers including ${codecount} codes, and ${printcount} printables as of ${n}`
Gawel1908
  • 138
  • 1
  • 9