Question

Assignment-2(2).pdf + X A file:///C:/Users/bhati/OneDrive/Desktop/Assignment-2(2).pdf 5 of 6 O + Fit to page IL Page view AV

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 string array and a character). The function will check each string in an array and count the number of instances of the character that was passed as second argument, if it is placed in an odd index position in that string, regardless of case. The counts for each element of an array are concatenated as one string separated by hyphen and returned E.G. function([“AaA123Aa”, ”Hannah”, ”1090aambcaA”], “A”); // returns: ‘2-1-2’

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

Please comment if you have any doubt

Q1)

<script type="text/javascript">
   function sumOfAscii(arr) {
       //sum of ascii values
       sum=0;
       for(i=0;i<arr.length;i++){
           //converting the value at position to string value
           arr[i]=arr[i]+'';
           //traversing through each value in the position
           for(j=0;j<arr[i].length;j++){
               //charCodeAt() will return ascii value of the element
               sum+=arr[i].charCodeAt(j);
           }
       }
       //returning the sum
       return sum;
   }
   document.write(sumOfAscii(['A','bc',12]));
</script>

function sumofAscii(arr) { //sum of ascii values sum=0; for(i=0;i<arr.length;i++){ //converting the value at position to stri

output:

361

Q2)

function fun(arr,char){
str="";//empty string to store count of char
//traversing through array
code=char.charCodeAt(0);
upper=lower=code;
if(code>=65 && code<=90){
upper=code;
lower=code+32;
}
else if(code>=97 && code<=122){
lower=code;
upper=code-32;
}
for(i=0;i<arr.length;i++){
c=0;//count of char in the string
for(j=0;j<arr[i].length;j++){
//in case of a odd index
//if the index value match with char
//count is incremented
if(j%2!=0)
if(arr[i].charCodeAt(j)==upper || arr[i].charCodeAt(j)==lower)
c++;
}
//concatnating count and '-'
if(i!=arr.length-1)
str+=c+"-";
else
str+=c;
}
//returning the string without last '-'
return str;
}
document.write(fun(["AaA123Aa", "Hannah", "1090aambcaA"], "A"));

<script type=text/javascript> function fun(arr,char){ str=;//empty string to store count of char //traversing through arr

output:

2-1-2

function sumofAscii(arr) { //sum of ascii values sum=0; for(i=0;i<arr.length;i++){ //converting the value at position to string value arr[i]=arr[i]+""; //traversing through each value in the position for(j=0; j<arr[i].length;j++){ //charCodeAt() will return ascii value of the element sum+=arr[i].charCodeAt(j); y } //returning the sum return sum; } document.write(sumofAscii(['A', 'bc',12]));

Add a comment
Know the answer?
Add Answer to:
CODE THE FOLLOWING FUNCTIONS IN JAVASCRIPT (USE OF THE ABOVE MENTIONED IN BUILT 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 function in JAVASCRIPT (Use of the given list of inbuilt functions is not...

    Code the following function in JAVASCRIPT (Use of the given list of inbuilt 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’ Assignment-2(2).pdf + х A file:///C:/Users/bhati/OneDrive/Desktop/Assignment-2(2).pdf 5 of 6 O + Fit to page IL Page...

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

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

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

  • No loops can be used and must only use functions that are built into matlab, aka...

    No loops can be used and must only use functions that are built into matlab, aka no toolboxes FACTORS All Factors of an Integer FACTORS(N) returns all the factors of the positive integer N. For example: FACTORS(60) returns the 2 X 6 array [ 1 2 3 4 5 6 60 30 20 15 12 10] FACTORS(25) returns the 2 X 2 array [ 1 5 25 5] FACTORS(7) returns the 2 X 1 array [1 7] If N is...

  • language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or...

    language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or Wrapper classes. In general, you may not use anything from any other Java classes, unless otherwise specified. You are not allowed to use String literals in your code ("this is a string literal"). You are not allowed to use String objects in your code. The methods must be implemented by manipulating the data field array, The CSString Class: NOTE: Pay very careful attention to...

  • Rules: You may NOT use the following built-in functions. sort() sum() max() min() Except for format()...

    Rules: You may NOT use the following built-in functions. sort() sum() max() min() Except for format() all string methods are banned. Except for append() and extend() all list methods are banned. You may not import any module. Make sure to test your code with a variety of inputs. Do not assume the examples in these directions are reflective of the hidden test cases. Part 3 Echo (5 Points) Write a function called echo that takes as argument a list and...

  • Can someone please help me with this javascript - Javascript - do at least 10 of...

    Can someone please help me with this javascript - Javascript - do at least 10 of the following questions below ----------------------------------------------------------------------------------- Create an HTML file that takes in input and carries out of the following functions and manipulates the DOM to show the outcome. Please put the question itself as a comment above each answer. Use either JavaScript, jQuery, or both (but know the difference!). ----------------------------------------------------------------------------------- 1. Fibonacci Define function: fib(n) Return the nth number in the fibonacci sequence. 2....

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