Skip to content

Instantly share code, notes, and snippets.

@padolsey
Created July 18, 2012 07:47
Show Gist options
  • Save padolsey/3134886 to your computer and use it in GitHub Desktop.
Save padolsey/3134886 to your computer and use it in GitHub Desktop.
JS challenge

Implement Point so that:

new Point( new Point(10, 20) + new Point(30, 50) ).toString() === '{40,70}'

Must be generic and be able to handle x and y values from 0..999 (integers only)

Minor hint: you'll need to implement valueOf and toString methods.

@mmorton
Copy link

mmorton commented Jul 18, 2012

@tsvetomir
Copy link

Yaay :) Thanks for the exercise.

It would've been great if we could override operators in JS. Oh, well :)

@padolsey
Copy link
Author

@azproduction, @mmorton, @tsvetomir, nice work! Three really different and cool techniques! :)

@davidaurelio
Copy link

@padolsey
Copy link
Author

@davidaurelio, cool!

So, so far we have two string-based solutions and two numerical. The string-based solutions theoretically allow floating-points with decimals while the numerical ones allow general arithmetic operators.

@davidaurelio
Copy link

Another solution using two 16bit fixed point numbers. Has problems with overflow, though: https://gist.github.com/3135390

@avipinto
Copy link

mine: https://gist.github.com/3138260
it was fun :)

@dundalek
Copy link

Trying to make it as shortest as possible, which is an usual goal of theese challenges :)
https://gist.github.com/3139898
(String-based solution, handles floats)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment