<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 sum = 3 + 5;
console.log(sum);
// Insert your code for #2 here
var halloween = prompt ("How many days until Halloween?");
console.log("the variable 'halloween' is of type" +typeof halloween);
halloween = Number(halloween);
console.log("After Number() the variable 'halloween' is of type" +typeof halloween);
halloween = halloween+1
console.log("The amount of days until All Souls Day, then, is" + halloween);
// Insert your code for #3 here
var x = Math.sqrt(169); //I'm attempting to tell Java to find the square root of 169 which is 13
console.log(x);
// Insert your code for #4 here
var str = prompt ("Type a word here!");
var n = str.length;
console.log(n); //I'm attempting to have someone put any word into the prompt and the Java will tell the length of that word using the string length property
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.