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

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

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

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

  • Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to...

    Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to implement a binary search algorithm to determine whether a given element is a member of a given sequence Each function will have two parameters, aseq, a sorted sequence, and target. isMemberR and isMemberI will return True if target is an element of the sequence, and False otherwise. Your implementations must implement the binary search algorithm described above. When function i sMemberR recursively invokes itself,...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • Please place all of the following functions (defun …) into a SINGLE .lsp file As such,...

    Please place all of the following functions (defun …) into a SINGLE .lsp file As such, be sure to use the EXACT SAME function names and parameter numbers and orders I provide ​ Write a function ONEFIB that takes a single parameter n and returns the nth Fibonacci number. Write a function ALLFIB that takes a single parameter n and returns a list of the first n Fibonacci numbers. Do not worry about efficiency here. HINT: You can use ONEFIB...

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