Minimum Calculation
var input1 = prompt("Enter the first number:");
var input2 = prompt("Enter the second number:");
function min(x,y){
x = Number(x);
y = Number(y);
if (x<y){
return x;
}
else if (y<x){
return y;
}
else if (x===y){
return x;
}
else {
return null;
}
}
document.write(“
The minimum of the two numbers is: “,min(input1,input2),”
”)Recursive odd/even
var input1 = prompt("Enter the integer to test for odd/even:");
function odd_even(x){
x = parseInt(x);
if (x < 0){
x = Math.abs(x);
}
if(x===0){
return "even";
}
else if(x===1){
return "odd";
}
else {
x = x - 2;
}
return odd_even(x);
}
document.write("<h3>The number ",input1," is: ",odd_even(input1),"</h3>");
Bean counting
var input1 = prompt("Enter the string to consider:");
var input2 = prompt("Enter the character to count:");
function count_char(str,char){
var count = 0;
for(var i = 0;i<str.length;i++){
//console.log("i = ",i, "char =",str[i]);
if (str[i]==char) count++;
}
return count;
}
document.write("<h3>The number of ",input2,"'s"," in '",input1,"' is: ",count_char(input1,input2),"</h3>");