Skip to content

Instantly share code, notes, and snippets.

@dubistdu
Last active May 8, 2019 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dubistdu/c0d345770eba96628b2e45bb6e24c740 to your computer and use it in GitHub Desktop.
Save dubistdu/c0d345770eba96628b2e45bb6e24c740 to your computer and use it in GitHub Desktop.
perimeter_of_squares_in_a_rectangle

ruby

def perimeter(n)
  num = [0,1]
  i = 0
  until i == n do
    num << num[i] + num [i+1]
    i += 1
  end
  num.sum * 4
end

js

function perimeter(n) {
  let arr = [0,1];
  for(let i = 0; i < n ; i++) {
    arr.push(arr[i] + arr[i+1]);
  }
  return 4 * arr.reduce((sum, num) => sum + num, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment