# A Simple Javascript Program Interpreter

 Program Error Messages 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 // This program considers a hoizontal segment AC of length // x feet and a point B above it, where the angle CAB is a degrees // and ACB is b degrees. Taking x,a,b as input, in computes and // displays the height y (in feet) of point B above segment AC. // Get length x and angles a and b var x = prompt("Enter length x (in feet)"); var a = prompt("Enter angle a (in degrees)"); var b = prompt("Enter angle b (in degrees)"); // Convert angles a and b to radians var ar = a/180*Math.PI; var br = b/180*Math.PI; // Compute and print height var y = x*Math.tan(br)/(1 + Math.tan(br)/Math.tan(ar)); alert("y = " + y + " feet");
Javascript Cheat Sheet
• arithmetic: + - * / %←means remainder. There's no ^ for exponentiation!
• math functions: Math.sin( ), Math.cos( ), Math.log( ), Math.exp( ), Math.sqrt( ), Math.floor( )
• math constants: Math.PI, Math.E
• variables: define like var x; or var x = 0;. Assign like x = x + 1;
• semicolon: use ; to terminate statements, e.g. var x; x = 17;
• string literals: "hi" or 'hi' or 'I said "hi", right?'
• escaping: within a string literal, \" for ", \' for ', \\ for \, \n for newline
• string operations: "a" + "b"ab, "foobar"[3]b, "foobar".length6
• strings and ASCII:
• typeof: returns type name as a string, e.g. typeof(43*7)Number
• conversions: String-to-Number String(42), Number-to-String Number("42")