Imagine this, we have an environment with a global scope containing just a single object, called codegolf
. This object has a single child called stackexchange
, which has a property called com
.
Accessing this property would look like codegolf.stackexchange.com
.
The challenge
The input of your program/function will be a string trying to access a property on the global scope. Whenever this property is found, you shall print/return a truthy value. If the property isn't found, a falsy value shall be printed/returned. The catch: when you try to access a property on a non-existant object, your program should throw any kind of error¹.
To make things a bit easier, you may assume that input will always be [a-z.]
, it will never be empty, it will never have repeating .
's and it will never start or end with a .
. So codegolf.
is an invalid input.
Test cases
codegolf.stackexchange.com => 1 // or any other truthy value
codegolf.stackexchange.net => 0 // or any other falsy value
codegolf.stackexchange => 1
codegolf.foo => 0
codegolf => 1
foo => 0
codegolf.com => 0
codegolf.constructor => 0
codegolf.foo.bar => Error (since foo is undefined)
codegolf.stackexchange.com.foo => Error (since com is a value, not an object)
codegolf.stackexchange.com.foo.bar => Error
foo.stackexchange.com => Error
foo.bar => Error
foo.bar.baz => Error
This is code-golf, shortest code in bytes wins
¹ if (and only if) your language of choice doesn't support errors at all, you must output something which makes it clear that this is an error. For example, if you use 1 for truthy and 0 for falsy, you may use "e" for an error. Be consistent in your return values and explain the behaviour in your post.
foo => Error
would be more appropriate. \$\endgroup\$codegolf.com
to the test cases to rule outcodegolf(.stackexchange)?(.com)?$
type checks \$\endgroup\$foo.stackexchange.com
\$\endgroup\$foo
would returnundefined
, but it wouldn't throw an error.foo.bar
would throw an error becausefoo
is not defined. \$\endgroup\$codegolf.foo => 0
, sofoo => 0
. \$\endgroup\$