We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functionformingMagicSquare(s){// Write your code hereconstsquare1=[[8,1,6],[3,5,7],[4,9,2]];constsquare2=[[6,1,8],[7,5,3],[2,9,4]];constsquare3=[[4,9,2],[3,5,7],[8,1,6]];constsquare4=[[2,9,4],[7,5,3],[6,1,8]];constsquare5=[[8,3,4],[1,5,9],[6,7,2]];constsquare6=[[4,3,8],[9,5,1],[2,7,6]];constsquare7=[[6,7,2],[1,5,9],[8,3,4]];constsquare8=[[2,7,6],[9,5,1],[4,3,8]];functioncalculateDifference(s,t){constdiff=Math.abs(s[0][0]-t[0][0])+Math.abs(s[0][1]-t[0][1])+Math.abs(s[0][2]-t[0][2])+Math.abs(s[1][0]-t[1][0])+Math.abs(s[1][1]-t[1][1])+Math.abs(s[1][2]-t[1][2])+Math.abs(s[2][0]-t[2][0])+Math.abs(s[2][1]-t[2][1])+Math.abs(s[2][2]-t[2][2])returndiff}constcount1=calculateDifference(s,square1);constcount2=calculateDifference(s,square2);constcount3=calculateDifference(s,square3);constcount4=calculateDifference(s,square4);constcount5=calculateDifference(s,square5);constcount6=calculateDifference(s,square6);constcount7=calculateDifference(s,square7);constcount8=calculateDifference(s,square8);constcounts=[count1,count2,count3,count4,count5,count6,count7,count8]returnMath.min(...counts)}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Forming a Magic Square
You are viewing a single comment's thread. Return to all comments →
My JavaScript Solution