Practice Set - Basic Math and String Operations
for CSCI E3, Harvard University author(s): Larry Bouthillier
by DustyWhite
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
// SHORT script to calculate sum and output in console:
console.log("The sum of the numbers 5 and 15 is . . . ");
console.log(5 + 15);
// Insert your code for #2 here
// Adding [+1] to user input after prompt; output to console:
var userNumber = prompt("Please enter a number of your choice:");
console.log(++userNumber);
// Insert your code for #3 here
// Getting square root of (adjusted) variable "userNumber +1" and outputting that to the console:
console.log("The square root of the sum of the number input by the user and 1 is: " + Math.sqrt(userNumber));
// Insert your code for #4 here
// Prompting for word and counting the letters in that word: outputting to console:
var funnyWord = prompt("Please enter a word your think is fun to say:");
console.log("Your word has " + funnyWord.length + " letters, but my algorithms have deduced that " + funnyWord + " isn't very funny. Please try again.");