Practice Set - Basic Math and String Operations

by somya_kashyap3

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

// we caculate the sum using a variable sum and simply adding 1,2 there can be other alternatives to this question
var sum = 1 + 2 ;
console.log("sum is " + sum) ;


// prompt user for a number 
var a = prompt("enter a number");
// I have used the increment operator before the operand in which the operation excutes and adds 1 to the number before returning it
console.log(++a);


// randomly choose any number preferably I chose a non zero integer
var b = 3;
// we use the Math.pow() function to evaluate the square the number and get the output to the console
console.log(Math.pow(b,2));


// string length can be calcualted using length property
var c = prompt("enter a string")
console.log("string length is " + c.length)