Skip to content

Instantly share code, notes, and snippets.

@lalosh
Created November 25, 2021 12:05
Show Gist options
  • Save lalosh/ed4981a74c1daafb539f73f673a8f218 to your computer and use it in GitHub Desktop.
Save lalosh/ed4981a74c1daafb539f73f673a8f218 to your computer and use it in GitHub Desktop.
console.log('challenge accepted');
let x = {
// save the real value
_value: 0,
// get the value implicity when needed
valueOf: function getX() {
return (x._value += 5);
}
}
// while x is object it's not comparable to a number, so a (valueOf) function is exuectued(if it is defined)
if (x == 5 && x == 10 && x == 15) {
console.log('Challenge Solved');
}
// take care for subsequent x values
if(x == 20){
console.log('yes, x keeps incrementing forever whenever js implicity request its value');
}
// x = {"_value":20} no matter how many you print x this way
// because it trigger (toString) and not (valueOf)
console.log('x =', JSON.stringify(x));
console.log('x =', JSON.stringify(x));
console.log('x =', JSON.stringify(x));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment