Basically your code is correct.
If block with id "test" has attribute "style" with backgroundColor, written inline, it should work. It should be something like:
<div id="test" style="background-color: red"> </div>
If you declare background-color it css style your js will not work.
However next one color identification will work in both situations:
var color = window.getComputedStyle(elem).getPropertyValue('background-color');
So try to debug it using web-inspector.
Make sure variable "color" contains needed color - add
console.log(color)
and see result in console.
Also check "elem" and "elem2", maybe you have errors in ids.