111/p1/rectangle.js This task is an exercise in writing and testing JavaScript f
ID: 3778323 • Letter: 1
Question
111/p1/rectangle.js
This task is an exercise in writing and testing JavaScript functions in the Chrome Console.
Write a program that prompts the user using JavaScript prompt() (Links to an external site.) for the area and width of a rectangle and then displays the length, the perimeter and the diagonal. Hint: Math.sqrt() (Links to an external site.), also you'll need to use parseInt() or parseFloat() to convert the string returned from prompt to a number.
Output specification:
area = 12
width = 3
length = 4
perimeter = 14
diagonal = 5
The output should be displayed using both the alert function and the console.log function.
Note: You can use the newline character (' ') in a string to format the string across two lines in both alert and console.log.
111/p1/type.js
Write a program that includes a function that accepts a single parameter, and returns the datatype of the function parameter.
Output specification:
The function argument is of type nn
The nn will be one of the primitive data types (see p. 114 of the course textbook), or Object.
Your program should include individual calls to your function with function argument values representing each of the primitive data types, a Date object, and a String object (see p. 117 of the course textbook to see how to create values of these types).
The output should be displayed using both the alert function and the console.log function.
Hint: You will need to use string concatenation to build your return result.
Explanation / Answer
Program1)
var area = parseFloat(prompt("Enter area:"));
var width = parseFloat(prompt("Enter width:"));
var length = area/width;
var perimeter = 2*(length+width);
var diagonal = Math.sqrt(Math.pow(width,2)+Math.pow(length,2));
alert("Length:"+length+" Perimeter:"+perimeter+" Diagonal:"+diagonal);
console.log("Length:"+length+" "+"Perimeter:"+perimeter+" Diagonal:"+diagonal);
Program2)
function getType(x){
alert("The function argument is of type "+typeof(x));
console.log("The function argument is of type "+typeof(x));
}
getType(10);
getType(null);
getType("Hello");
/* sample output
The function argument is of type number
The function argument is of type object
The function argument is of type string
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.