Skip to content

Instantly share code, notes, and snippets.

@samccone
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samccone/34e275e87aa1fd50abee to your computer and use it in GitHub Desktop.
Save samccone/34e275e87aa1fd50abee to your computer and use it in GitHub Desktop.
{"title":"Finding the norm of a vector in Javascript","author":"jmeas","pages":[{"pageName":"","sections":[{"type":"text","source":"The norm, or magnitude, of a vector, is its length. In two dimensions, this can be represented in the following way:\n\n$$\\lVert v \\rVert = \\sqrt{x^2+y^2}$$\n\nIn Javascript, we could write a function to calculate this like so:\n\n```js\nfunction norm(x, y) {\n return Math.sqrt(x*x + y*y);\n}\n```\n\nLet's see an example of that:"},{"type":"javascript","source":"// Define the norm\nfunction norm(x, y) {\n return Math.sqrt(x*x + y*y);\n}\n\n// Calculate it\nvar val = norm(3, 4);\n\n// Set it to the output div\nvar output = document.getElementsByClassName('output')[0];\noutput.innerHTML = val;"},{"type":"html","source":"<!-- This is just a div to show our output -->\n\n<div class='output'></div>"},{"type":"css","source":"/* Let's give it some styling, too */\n\n.output {\n background: #eee;\n border: 1px solid #ddd;\n padding: 20px;\n}"}]}],"public":true}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment