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
  • 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...

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
ADVERTISEMENT