Interview Questions on Javascript
Quick Questions to check basic conceptual knowledge.
Question 1
Given an string print the characters with their number of occurrences Input : aaccceezzzzqaaa Output : a2c3e2z4q1a3 The simplest way to write a code for this scenario is, using a nested for loop. Check each character’s occurrences within a string, keep a counter of the same and store either as a key value pair or append in a string (as shown below). This concept is a general one and can be used across any language.
Javascript:
let input = 'aaccceezzzzqaaa';
let length = input.length;
let count;
let output = '';
console.log('Given an string print the characters with their number of occurrences');
console.log('Input : ' + input);
for(a=0; a<length; a++) {
count = 1;
for(b=a+1; b<length; b++) {
if (input.charAt(b) === input.charAt(a)) {
count++;
if (b === length - 1) {
a = length;
}
} else {
output = output + input.charAt(a) + count;
a = b - 1;
b = length;
}
}
}
output = output + input.charAt(length-1) + count;
console.log('Output : ' + output);