Targets and Vicinities
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Description
You're playing a game with a friend called Target and Vicinity. Target and Vicinity consists of one person thinking up a number in their head, and the other guessing that number.
Sounds simple enough-- the caveat is that we also want to credit close guesses. To estimate the proximity of the guess to the actual number, we use the concept of "targets" and "vicinities" in comparing accuracy.

It works like this: targets are digits in the guessed number that have the same value of the digit in actual at the same position. Here's an example of two targets:
Actual: "34"
Guess: "34"
"2T0V"
Notice that both the actual number and the guess have the same values, in the same position, with regards to 3 and 4.
Now onto "vicinities". Vicinities are digits in guess that have the same value as some digit in actual, but don't share the same position (hence the nomenclature).
Person 1 has to tell person 2 how many targets and vicinities there are by providing a string in this format:
`${number of targets}T${number of vicinities}V"
Here are some examples of inputs and ouputs:
const actual = "345"
const guess = "135"
// "1T1V"
// Because `5` is a T, and `3` is a V
const actual = "45624"
const guess = "24325"
// "1T2V"
// Because `2` is a T, and `4` and `5` are Vs
You're given two strings, an actual and a guess, as parameters. Write a function to return the number of targets and vicinities in the above string format. You may assume the strings are of the same length.
Constraints
- The actual and guess strings will be of length <=
100000 - The strings will only consist of numeral digits
0-9 - Expected time complexity :
O(n) - Expected space complexity :
O(n)
```js
function getTV(actual, guess) {
let targets = 0;
let vicinities = 0;
// fill this in
return `${targets}T${vicinities}V`
};
```
CONTINUE WITH ALGODAILY PREMIUM
Keep going with the complete tutorial.
Unlock the remaining explanations and exercises, alongside the full learning library.
Explore Premium →