Practice Set - Basic Math and String Operations

by Angela Baruth

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

// Write a script to add two numbers and output the sum to the console 
// example 1

// declare and set my variables
alert("two numbers and output the sum");
var number1 = 5;
var number2 = 16;

// do some addition and assign the result to a new summery
var addNumbers = number1 + number2;

// logged to the console.
// output that result: 5+16= 21 summery
console.log("5+16= " + addNumbers + " summery");

// example 2  
// declare and set my variables
alert("how long must I work");
var myAgenow = 44;
var stoptowork = 60;

// do some subtraction and assign the result to a new variable
var result = stoptowork - myAgenow;

// logged to the console.
// output that result: I will be work 16 years then 60 years old.
console.log("I will be work " + result + " years then 60 years old.");

//example 3
// logged to the console.
// output that result: 11
alert("put out the sum on the console 5 + 6 ");
console.log(5 + 6);



// 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). #2 here

// example 1

// declare and set my variables 
var myAgenow = 44;
var stoptowork = 60;

// do some subtraction and assign the result to a new variable
// using the '-' operator to concatenate two numbers 
var result = stoptowork - myAgenow ;

// Output that result: I will not more work until in 16 years then I'm 60 years old.
// logged to the console.
prompt("type my age 44");
// logged to the console
console.log("I will not more work until " + result + " years then I'm 60 years old.");


// example 2

// declare and set my variables
var year = prompt("Tpe a year and you see the result after 16 years?");
// declare and set my variables
year = Number(year);
// summery from 2016 and 16 years later
year = +year + 16;
// logged to the console.
// output that result: 2032
console.log("In 16 years its correct " + year+ "...