Practice Set - Basic Math and String Operations

by Ramya Ranganathan

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
    <div id="Add"></div>
    </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).
    <div id="Increment"></div>
    </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.
<div id='console'>
</div>
    <div id="FindSquareRoot"></div>
    </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.
    <div id='console1'>
</div>

<div id="StringLength"></div>
    </li>
</ol>

JavaScript

// Insert your code for #1 here

var val1 = 5;
var val2 = 2;

 //check that they are integers, otherwise make them zero
 val1 = (parseInt(val1) ? parseInt(val1) : 0);
 val2 = (parseInt(val2) ? parseInt(val2) : 0);
 
// add Two Numbers
var total = val1 + val2

document.getElementById('Add').innerHTML = total;

// Insert your code for #2 here

var a = Number(prompt("Enter a Number to Increment by 1"));

a++
   
b = a

document.getElementById('Increment').innerHTML = b;


// Insert your code for #3 here

/////////////////////////////////////////////////////////////

var jsFiddleConsole = function() {
    return({
        log: function(msg) {
          consoleDiv = document.getElementById('console');
          para = document.createElement('p');
          text = document.createTextNode(msg);
          para.appendChild(text);
          consoleDiv.appendChild(para);
        }
    });
}();

////////////////////////////////////////////////////////////

// Finding Square Root of a Number

var x = Number(prompt("Enter a Number to Find Square Root"));

var y = Math.sqrt(x);      // Returns the square root of Input

jsFiddleConsole.log('Square Root of Input:');

document.getElementById('FindSquareRoot').innerHTML = y;


// Insert your code for #4 here


/////////////////////////////////////////////////////////////

var jsFiddleConsole = function() {
    return({
        log: function(msg) {
          consoleDiv = document.getElementById('console1');
          para = document.createElement('p');
          text = document.createTextNode(msg);
          para.appendChild(text);
          consoleDiv.appendChild(para);
        }
    });
}();

////////////////////////////////////////////////////////////
var str = String(prompt("Enter a String"));

//var str = "Hello World!";
var n = str.length;

jsFiddleConsole.log('Length of Input String: ');

document.getElementById('StringLength').innerHTML = n