Skip to main content
Participating Frequently
October 13, 2022
Answered

Help with adding variable to strings

  • October 13, 2022
  • 3 replies
  • 670 views

Hello community! I'm trying to add a variable value to a string but my only experience with this are using the backticks and that doesn't seem to work. Here is what I have:

 

const t175CM = Number(this.getField("t175CultureMiddle").value);

const vIsletM = Number(this.getField("volumeIsletMiddle").value);

const cARm = (t175CM * 30) - vIsletM;

if (cARm < 0) {
    const removed = '${cARm} mL-removed';
    event.value = removed;
} else {
    const added = '${cARm} mL-added';
    event.value = added;
}

 Would appreciate some help. I got the impression in some other searches I need use a document level function but I'm probably not the sharpest knife in the drawer and not sure what that actually means.

 

Thanks in advance!

This topic has been closed for replies.
Correct answer Bernd Alheit

You can add text with

cARm + "text"

3 replies

Legend
October 13, 2022

That format is used in Perl and shell scripting.  Make sure your learning materials are specifically for JavaScript. 

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
October 13, 2022

You can add text with

cARm + "text"

Participating Frequently
October 14, 2022

Thanks! This worked though I swear I tried this before! The working code is this for me.

 

var t175CM = Number(this.getField("t175CultureMiddle").value);

var vIsletM = Number(this.getField("volumeIsletMiddle").value);

var cARm = (t175CM * 30) - vIsletM;

if (cARm < 0) {
    event.value = cARm + " mL-removed";
} else {
    event.value = cARm + " mL-added";
}

 

Nesa Nurani
Community Expert
Community Expert
October 13, 2022

Replace 'const' with 'var'.

also these lines won't work

'${cARm} mL-removed';

and

'${cARm} mL-added';