Practice Set - Basic Math and String Operations
by Chetak Patel
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
//Basic Math and String Operations
//4 Tasks for this Practice Set:
//Problem #1 - Adding Two Numbers
"use strict";
var total
var x = 20
var y = 15
total = y + x
console.log("Your total X + Y = " + total);
//results will show up in console output
// Insert your code for #2 here
//Basic Math and String Operations
//4 Tasks for this Practice Set:
//Problem #2 - Prompt and adding 1
"use strict";
var Banana = prompt("You have 1 Banana how many more Bananas would you like?");
Banana = Number(Banana);
Banana = Banana +1;
console.log("Your total Bananas are " + Banana);
//results will show up in console output
// Insert your code for #3 here
//Basic Math and String Operations
//4 Tasks for this Practice Set:
//Problem #3 - Roundung up the nearest whole number. Using Math.ceil
"use strict";
var rounder = prompt("Please enter a number to first decimal point and our Rounder will round up to the new nearst whole number for you. Exmaple 6.7 will give you 7.");
rounder = Number(rounder);
var rounder = Math.ceil(rounder)
console.log("The nearest whole number is " + rounder);
//results will show up in console output
// Insert your code for #4 here
//Basic Math and String Operations
//4 Tasks for this Practice Set:
//Problem #4 - You will be prompt to put your first and last name in 2 different dialogs and the String object will put them to gether and out put your name.
"use strict";
var str1 = prompt("Please enter your First Name");
var str2 = prompt("Please enter your Last Name")
var str1 = str1;
var str2 = str2;
var res = str1.concat (str2);
console.log("Your Name is", res);
//results will show up in console output