I know that you can use ${} inside a string to put a var but here it seems it doesn't want to recognise the var and it is not in the brackets
var str = s.toLowerCase();
var counts = {};
var ch, index, len, count;
for (index = 0, len = str.length; index < len; ++index) {
ch = str.charAt(index);
count = counts[ch];
counts[ch] = count ? count + 1 : 1;
}
for (ch in counts) {
console.log('${ch}(${counts[ch]})');
}
3 Answers 3
ES2015 template literals require a back tick:
console.log(`${ch}(${counts[ch]})`);
Working example:
const s = 'An example string with some duplicate characters.';
var str = s.toLowerCase();
var counts = {};
var ch, index, len, count;
for (index = 0, len = str.length; index < len; ++index) {
ch = str.charAt(index);
count = counts[ch];
counts[ch] = count ? count + 1 : 1;
}
for (ch in counts) {
console.log(`${ch}(${counts[ch]})`);
}
answered Jul 29, 2017 at 17:57
PeterMader
7,3552 gold badges24 silver badges32 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Яни Георгиев
the problem is not in the brackets
PeterMader
What do you mean? Where is it, then?
Яни Георгиев
i want to be inside 1 string
PeterMader
There's only one string literal. Could you please clarify what the result should look like?
You need to use the back tick. Use ` instead of '.
Comments
You need to put your string inside ``, not ''.
var myVar = 'hello world!';
console.log(`${myVar}`);
answered Jul 29, 2017 at 17:56
David Koplik
211 gold badge1 silver badge7 bronze badges
Comments
lang-js
'with`