DEV Community

Discussion on: The challenge to make a shape area calculation in CodeSignal

Collapse
 
_bigblind profile image
Frederik πŸ‘¨β€πŸ’»βž‘οΈπŸŒ Creemers

I think think there's an even easier solution:

shapeArea = function(n){
    return 1 + (n-1)*4
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
xerghos profile image
Xerghos

I think this is more appropriate:

function shapeArea(n) {
return (1 + (n-1)*n) *2 - 1
}

Collapse
 
trungsusi97 profile image
TrungSuSi97

Why do you think that formula ?

Thread Thread
 
fromchiapasdev profile image
Axel Espinosa

Did you solve it?

Collapse
 
fromchiapasdev profile image
Axel Espinosa

Hey buddy, can you explain your solution?

Collapse
 
equiman profile image
Camilo Martinez

Yeah, that's exactly the beauty that I talk about code fights. You can learn from other solution.

Collapse
 
kikeflowers profile image
kikeflowers • Edited

I found this solution and just replaced the variables

function shapeArea(n) {
return 1 + 2 * n * (n-1)
//1 + 2 * 4 * (3) = 3*4=12*2=24+1=25
//1 + 2 * 3 * (2) = 2*3=6*2=12+1=13
//1 + 2 * 2 * (1) = 1*2=2*2=4+1=5
//1 + 2 * 1 * (0) = 0*1*=0*2=0+1=1
}