Practice Set - Basic Math and String Operations

by Angeli Schwartz

HTML

<h3>Basic Math and String Operations</h3>
<h4>4 Tasks for this Practice Set:</h4>
<ol>
    <li>
        Write a script to add two numbers and output the sum to the console</li><li>
    Write a script that prompts for input, adds one to whatever number was input and outputs the sum to the console. Use the increment operator.  Assume that the input will be a number (no type-checking is required for this exercise).
    </li><li>
    Look up the Math object, and use one of its methods to do <i>something</i> to a number and output the result to the console (you pick: square it, round a decimal number up or down, do a trig function...whatever) Make sure the you comment your code or use console output to describe what you’re doing.
</li><li>
    Prompt for input and assign the result to a variable. Then, look up the String object, and use one of its properties to output the length of the string variable to the console.
    </li>
</ol>

JavaScript

// Insert your code for #1 here
// Assign value of x and y variables
var x = 10;
var y = 15;

//do some addition and assign the result to a new variable
var z = x + y;

//output that result
console.log(z);
//answer is 25

// Insert your code for #2 here
// Assign x and use increminator x++
var x = 10;
//increment operator
x++;
var z = x;
//output that result
console.log(z);
//answer is 11


// Insert your code for #3 here

var x = Math.sqrt(100);

//output that result
console.log(x);
// answer is 10

// Insert your code for #4 here

 var x = 5.656;
x.toExponential(2);     // returns 5.66e+0
//rounding to the next decimal number as answer
x.toExponential(5);     // returns 5.65600e+0
//adding two integers
x.toExponential(7);     // returns 5.6560000e+0
//adding four integers