Question

Code the following function in JAVASCRIPT (Use of the given list of inbuiltAssignment-2(2).pdf + х A file:///C:/Users/bhati/OneDrive/Desktop/Assignment-2(2).pdf 5 of 6 O + Fit to page IL Page view AV functions is not allowed)

Write a function that searches the string (passed as first argument) for the character (passed as second argument) and changes the case for all instances of that char found in string. The function should return the new string as shown in example below: function(‘abRA’, ‘a’) // returns ‘AbRa’

0 0
Add a comment Improve this question Transcribed image text
Answer #1

<script>
function search(string,character) {
   var result="";
   for(var i=0;i<string.length;i=i+1){
       if(string[i]==character){
   var asc_val=string[i].charCodeAt(0);
   if(asc_val>=65 && asc_val<=90){
   result=result+String.fromCharCode(asc_val+32);
}
else{
   result=result+String.fromCharCode(asc_val-32);
}  
}
else{
   result=result+string[i]
}
   }
return result
}
document.write(search('aaaBAchea','a')+"<br>");
document.write(search('aaaBAchea','A')+"<br>");
document.write(search('abRA','a')+"<br>");
</script>

JavaScript Result AAABAcheA aaaBachea ABRA 4 8 } 1 <script> 2 function search(string, character) { 3 var result=; for(var i

Please give a like

Add a comment
Know the answer?
Add Answer to:
Code the following function in JAVASCRIPT (Use of the given list of inbuilt functions is not...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JavaScript 1. Write a function which receives one argument (string) and converts the string to uppercase....

    JavaScript 1. Write a function which receives one argument (string) and converts the string to uppercase. function('abc') // returns 'ABC' 2. Write a function that accepts an array and a value. Find and return the first array element with bigger than given value. function([5,15,4],6) //returns 15 DO NOT USE THESE IN-BUILT FUNCTIONS BELOW endsWith() includes() indexOf() lastIndexOf() localeCompare() match() repeat() replace() search() slice() split() startsWith() substr() substring() toLocaleLowerCase() toLocaleUpperCase() toLowerCase() toString() toUpperCase() trim() valueOf() trimLeft and trimRight unshift() valueOf() includes()...

  • CODE THE FOLLOWING FUNCTIONS IN JAVASCRIPT (USE OF THE ABOVE MENTIONED IN BUILT FUNCTIONS IS NOT...

    CODE THE FOLLOWING FUNCTIONS IN JAVASCRIPT (USE OF THE ABOVE MENTIONED IN BUILT FUNCTIONS IS NOT ALLOWED) Write a function that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Write a function that accepts two arguments (a...

  • 6. Write a function which combines two strings which are passed as 7. The trimLeft) method remove...

    Note: Please test your output on the console and also avoid the functions below. We were unable to transcribe this image6. Write a function which combines two strings which are passed as 7. The trimLeft) method removes whitespace from the left end of a 8. Write a function which removes whitespace from both ends of the parameters. Mix one character from each string and return the concatenated result. function('ABC','Defg) return ADBeCfg string. Whitespace in this context is all the whitespace...

  • This project involves writing a program with functionality of your choosing. You must include the programming...

    This project involves writing a program with functionality of your choosing. You must include the programming elements as described below and must adhere to the general requirements below. Programming elements: The code must contain the following elements: (You may complete one program containing all of these elements or you may submit more than one program where all program together contain the following elements.) Classes (at least 3) Instance Fields and Methods Constructors Overloaded Method/Constructor (at least one) Arrays / ArrayLists...

  • Python Homework: - NOTE - Must not use built-in functions (other than the range() function), slice...

    Python Homework: - NOTE - Must not use built-in functions (other than the range() function), slice expressions, list methods (other than the append() method) or string methods in your solution. Write a function called reverse(my_list, number=-1) that takes a list and a number as parameters. The function returns a copy of the list with the first number of items reversed. Conditions: - The number parameter must be a default argument. -      If the default argument for number is given in...

  • 1. Implement a tostring() member function. This function will return a string representation of the LargeInteger....

    1. Implement a tostring() member function. This function will return a string representation of the LargeInteger. You should probably used string streams to implement this function. Note that the digits of the large integer are stored in reverse of their display order, e.g. the 1's place (100 ) is in index 0 of digits, the 10's place (101 ) is in index 1, etc. 2. Implement a second constructor for the LargeInteger. This constructor will be used to construct a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT