-3
return hex(id(self.table[hash]))

I do not know how to convert the following code from python to java script.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • 1
    Do you understand what that line of python code does? Once you do, figure out how to do each of those things in javascript. There's no way we can answer your question without more details, e.g. what is `self.table`? What is `hash`? Also, [`id` is a builtin python function](https://docs.python.org/3/library/functions.html#id) that returns a unique identifier for the object. In CPython, this is the object's memory address. I'm not sure how you'd replicate this in JS – Pranav Hosangadi Jan 04 '23 at 17:45
  • Pretty sure JS has no equivalent of Python's `id` function; can't swear to it, but I'm not aware of any. – ShadowRanger Jan 04 '23 at 17:45
  • 1
    If this is ever reopened, a note: Replicating Python's `id` function in JavaScript is already asked and answered in [unique object identifier in javascript](https://stackoverflow.com/q/1997661/364696), and converting to hex is addressed in [How to convert decimal to hexadecimal in JavaScript](https://stackoverflow.com/q/57803/364696). The rest of this is pretty trivial (assuming `hash` is an `int`, not the Python built-in `hash` function, and `self.table` is a `dict`, presized `list`, or some other appropriate sequence or mapping type). – ShadowRanger Jan 04 '23 at 20:54
  • 1
    @ShadowRanger It hasn't be re-opened. but has been asked [again](https://stackoverflow.com/questions/75028835/how-can-i-convert-this-python-line-of-code-to-javascript) and [again](https://stackoverflow.com/questions/75028844/how-can-i-convert-this-python-line-of-code-into-java-script) – phuzi Jan 06 '23 at 09:10

1 Answers1

1

You don't.

There is no equivalent code to that in JavaScript, since id returns the address of the object (in CPython anyway), and there's no way to do that in JavaScript.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • The Python language (as opposed to the CPython reference interpreter) just requires `id` to return an integer that is unique to that object and fixed for the lifetime of the object. I wouldn't expect any JS implementation to hand back raw memory addresses (compacting GC kinda ruins that), but they could provide some equivalent functionality. Pretty sure they *don't*, but I wouldn't focus on the memory address aspect so much. – ShadowRanger Jan 04 '23 at 17:48
  • 1
    @ShadowRanger There is no equivalent function in the JavaScript standard. – AKX Jan 04 '23 at 17:50