DEV Community

Cover image for AdventJS 2023: Day 6 Challenge

AdventJS 2023: Day 6 Challenge

Fenriuz on December 06, 2023

Solution to the Challenge #6 of AdventJS 2023 Solution to the previous challenge Solution to the next challenge Challenge Description Analys...
Collapse
 
alejandropicano profile image
Alejandro Miguel Pi Cano • Edited

function maxDistance(movements) {
let distance = 0, asterisks = 0;

for (let character of movements) {
if (character == ">")
distance++;
else if (character == "<")
distance--;
else
asterisks++;
}
if (distance < 0)
distance *= -1;
return distance + asterisks;
}

Collapse
 
fenriuz profile image
Fenriuz

Hi Alejandro! Great solution! This line if (distance < 0) distance *= -1; is the key, clever. Thanks for sharing!

Collapse
 
alejandropicano profile image
Alejandro Miguel Pi Cano

Hi, Thanks, at the beginning I used Math.Abs but I finally changed by distance *= -1 because the solution was scored better without Math library, I dont know why :)

Thread Thread
 
fenriuz profile image
Fenriuz • Edited

Sometimes it's because performance increased or cognitive complexity decreased with the change. πŸš€
Or sometimes the server performs better and gives you a better score. 🎲

Thread Thread
 
alejandropicano profile image
Alejandro Miguel Pi Cano

yes, youΒ΄re right