Practice Set - Basic Math and String Operations

by Tanveer Khan

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
   var firstNumber = 25;
   var secondNumber =35;
   var total = firstNumber + secondNumber;
   	console.log(total);


// Insert your code for #2 here
	 var someNumber = prompt("enter a number in the popup:"); //prompting the user with a pop to enter a number
	 someNumber = parseInt(someNumber); // making sure the variable is a integer 
	 someNumber = someNumber + 1; 
	   console.log("total is: :" + someNumber); //displaying the value after adding 1 to it
   


// Insert your code for #3 here
	 var srqRoot = Math.sqrt(36); // Using the Math object Math.sqrt to calculate the quare root of 36
	    console.log("Square root of 36 is: " + srqRoot); // calculating and displaying the square root of 36 in the console


// Insert your code for #4 here
	 
		var someText = prompt("Enter some text in the box"); // making the user enter some text in the popup
    	console.log("The variable 'someText' is of type " + typeof someText); // checking the variable type. I want it to be a string 
    someText = someText.length; // counting the length which the user enters in the popup
    	console.log("length of the text is : " + someText); // displaying the length of the text