Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1: The previous chapter introduced the standard function Math.max() that

ID: 3732947 • Letter: P

Question

Problem 1: 


The previous chapter introduced the standard function Math.max() that returns its largest argument. We can do that ourselves now. Write your own function max() that takes three arguments and returns their maximum. Do not use the Math.max() built-in function.  Submit JS file.


// Your code here. Output as follows. 


console.log(max(0, 10, 100));
// 100
console.log(max(0, -10, -100));
// -0


 


Problem 2: 


Write a JavaScript function that accepts two arguments, a string and a letter and the function will count the number of occurrences of the specified letter within the string. Output returned values to console.log(). Submit JS file.



Sample arguments : 'w3resource.com', 'o' 
Expected output : 2

Explanation / Answer

Solution Problem 1

// javascript function for myMax is provided

function myMax(num1, num2, num3) {
       var temp;
       if (num1 > num2)
           temp = num1;
       else
           temp = num2;
      
       var temp2;
       if (num3 > temp)
           temp2 = num3;
       else
           temp2 = temp;
          
       return temp2;          
   }

// console.log(myMax(a, b, c)) ... for running

Solution Problem 2

function countChars(str, chr) {
       var count = 0;
      
       for (var i=0; i<str.length; i++) {
           if (str.charAt(i) == chr)
               count = count+1;
       }
      
       return count;          
   }

// just call the function for running

// eg console.log(countChars('w3resource.com', 'o'))