In: Computer Science
/* complete javascript file according to the individual instructions given in the comments. */ // 1) Prompt the user to enter their first name. // Display an alert with a personal greeting for the user // with their first name (example: Hello, Dave!) // 2) Create a Try code block that will cause a ReferenceError. // Create a Catch code block that will display the error in the // console with console.error() // 3) Prompt the user to enter a number. If the user input is // not a number, throw a custom error to the console and alert // the user. // Solving a Problem! // 4) Create a function named productOf() that accepts // two numbers as parameters: productOf(num1,num2) // Create a Try block in the function that checks to see if // num1 and num2 are numbers. // If both parameters are numbers, return the product of // the two numbers: num1 * num2 // If either num1 or num2 are not numbers, throw a custom // error to the console and alert the user. // After defining the function, ask the user to enter a number. // Then ask the user to enter a second number. // Call the function with the values entered by the user. // Solving a Problem! // 5) Create a loop that counts from 1 to 20. // If the current number is even, add it to this empty array: var myArray = []; // If the current number is odd, log an odd number warning to // the console. // However, if the number equals 19, set a debug breakpoint. // After the loop completes, print the array to the console.
//1
var an=prompt("Enter your first name!");
alert("Hello, "+an+"!");
//2
try{
add();
}
catch(e){
console.error("Function is not defined!");
}
//3
function notAnumber(){
throw new Error('Entered value is not a number!'); //
Function notAnumber throws an error
}
var num=prompt("Enter a number:");
if(isNaN(num)) {
alert("Entered value is not a
number!");
console.error(notAnumber());
}
else {
alert("Entered number is :
"+num);
}
//4
function notAnumber(){
throw new Error('Entered value is not a number!'); //
Function notAnumber throws an error
}
function productOf(num1,num2) {
flag=0
try {
if(isNaN(num1) || isNaN(num2))
{
flag=1;
alert("Entered
value is not a number!");
}
else {
return
num1*num2;
}
}
catch(e) {
}
if(flag==1){
console.error(notAnumber());
}
}
var n1=prompt("Enter first number:");
var n2=prompt("Enter second number:");
alert(productOf(n1,n2));
//5
var myarray=[]
var j=0
for(i=1;i<=20;i++){
if(i%2==0){
myarray[j]=i;
j+=1;
}
else if(i==19){
debugger;
}
else{
console.log(i+" number is
odd");
}
}
console.log(myarray)