Codility-Solution

by Sandeep Kumar

HTML

<table>
        <tbody>
        <tr>
            <td style="color: #ff00ff; background-color:#FFFFFF">Q</TD>
            <td style="background-color: #442244; color: #442244">Y</td>
            <td style="color: #FFFF00; background-color:#442244">A</td>
        </tr>
        <tr>
            <td style="color: #FFEEFE; background-color:#990000">Q</td>
            <td style="color: #FFFF00; background-color:#FF0">M</td>
            <td style="color: #000000; background-color:#FF7777">O</td>
        </tr>
        </tbody>
    </table>

JavaScript

$( document ).ready(function() {
	alert(solution());
});

function solution() {
    // write your code in Javascript
    //
    // you can access DOM Tree using DOM Object Model:
    //    document.getElementsByTagName
    // or using jQuery:
    //    $('some_tag')
    //
    // please note that element.innerText is not supported,
    // you can use element.textContent instead.
    
    var outPutText = '';
  
    //Loop through each row of table.
    $('table').find('tr').each (function() {

        // Loop through of each cell of row.
        $(this).find('td').each (function() {
            // find current cell text.
            var cellText = $(this).html();
    
        	//Get background color and text color.
    		var $backColor = getHexValueFromRGB($(this).css("background-color"));
    		var $color = getHexValueFromRGB($(this).css("color"));
          
            //If background color and text color are same, then skip that cell text.
            if($backColor !== $color) {
                outPutText = outPutText + cellText;
            }
        });
    });
  
    return outPutText;
}

//Function to get hex format of a rgb color
function getHexValueFromRGB(rgbVal){
    var rgbArr = rgbVal.replace(/\s/g,'').match(/^rgba?\((\d+),(\d+),(\d+)/i);

    //Calculate hex value.    
    var hexVal = (rgbArr && rgbArr.length === 4)
                 ? "#" +
 	                ("0" + parseInt(rgbArr[1],10).toString(16)).slice(-2) +
 	                ("0" + parseInt(rgbArr[2],10).toString(16)).slice(-2) +
                    ("0" + parseInt(rgbArr[3],10).toString(16)).slice(-2)
                : rgbVal;

    //Return calculated hex value.
	return hexVal;
}