14
1
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.
1I feel like
foo => Error
would be more appropriate. – Magic Octopus Urn – 2016-11-30T15:13:22.660request to add
codegolf.com
to the test cases to rule outcodegolf(.stackexchange)?(.com)?$
type checks – colsw – 2016-11-30T15:21:02.440Another missing test case:
foo.stackexchange.com
– Jamie – 2016-11-30T15:22:37.033@carusocomputing Nope. Think about JavaScript.
foo
would returnundefined
, but it wouldn't throw an error.foo.bar
would throw an error becausefoo
is not defined. – mbomb007 – 2016-11-30T16:17:16.930@mbomb007 I mean, for Javascript yeah, for many other languages, that's not the case. – Magic Octopus Urn – 2016-11-30T16:48:01.613
1@carusocomputing True, but you can't say it's "more appropriate", when it makes sense both ways.
codegolf.foo => 0
, sofoo => 0
. – mbomb007 – 2016-11-30T16:52:42.527