Practice Set - Basic Math and String Operations

Week 3 assignment

by Larry Adams

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).
    <p id="demo"></p>
    
    </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

/*
"use strict";

var num1;
var num2;
var total = num1 + num2;
console.log(total);

alert("Week 3 assignment 1 input/output: Adding two numbers together");

num1 = prompt("Type in your 1st number", "");

num2 = prompt("Type in your 2nd number","");

total = (num1*1 + num2*1);

alert(num1 + " + " + num2 + " = " + total);

console.log("The total is " + total);

*/

// Insert your code for #2 here
/*
"use strict";

var age;
age++;

var nextYear = age;

alert("Question 2 incrementing");
age = prompt("Please enter your current age", "");

nextYear = ++age;
console.log("Your age for next year is " + nextYear);

*/

// Insert your code for #3 here
/*
var num1;
var num2;
var total = num1 + num2;
console.log(total);

alert("Question 3 rounding up your numbers");

num1 = prompt("Type in your 1st number", "");

num2 = prompt("Type in your 2nd number","");

total = (num1*1 + num2*1);

alert(num1 + " + " + num2 + " = " + total);

//Rounding up the total
console.log("The total is " + Math.round(total));

*/

// Insert your code for #4 here

alert("Question 4 string object");

var firstName = prompt("Type in your first name ", "");
var lastName = prompt("Type in your last name ", "");

person = (firstName  + lastName);
console.log("Person's name" + person);
console.log("This function counts the number of characters in their name " + person.length);