Count how many times sequence changes from one value to another

I have the following sequential array where a car changes colour. It starts red and is now yellow in the below example.

A = [red,blue,yellow,red,blue] 

I need to output is the number of times it went from each colour to each other colour.

output:

red-blue:2
blue-yellow:1
yellow-red:1

Can this be done in JS? Any help greatly appreciated!

Answer

function getColorChangeAmounts(colorChanges){
  const colorChangesMap = {};
  
  for(let i = 1; i < colorChanges.length; i++){
    const previous = colorChanges[i - 1];
    const current = colorChanges[i];
    const colorChangeText = previous + "-" + current;
    const currentCount = colorChangesMap[colorChangeText] || 0;
    
    colorChangesMap[colorChangeText] = currentCount + 1; 
  }
  
  return colorChangesMap;
}

const A = ["red", "blue", "yellow", "red", "blue"];
const colorChangesMap = getColorChangeAmounts(A);

console.log("output:");
console.log();
console.log(Object.entries(colorChangesMap).map(([key, value]) => key + ":" + value).join("n"))