Question

Write a javascript function that accepts 3 arrays and returns a string of the elements sorted...

Write a javascript function that accepts 3 arrays and returns a string of the elements sorted in ascending value, with duplicates removed. Return all alphabetic characters in lowercase. E.G. function([ 1,2,3, d], [5, 3, 0, ‘a’], [‘a’,’d’,9]) returns “0,1,2,3,5,9,a,d” E.G. function(['w', 'i', 'H', 5], [8, 5, 9, 'c', 'Z'], [6, 4, 'a', 'b', 'y']) returns “4,5,6,8,9,a,b,c,h,i,w,y,z” we must not use ' sort, remove, split,join and any built-in fucntions

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

Question

Write a javascript function that accepts 3 arrays and returns a string of the elements sorted in ascending value, with duplicates removed. Return all alphabetic characters in lowercase.

Input

3 arrays

Output

A string containing

a) all the elements in all the three arrays

b) sorted in ascending order

c) duplicates removed

d) lowercase characters

Constraint

Should not use in-built functions like  sort, remove, split, join, etc

Approach

There are three arrays :

array1, array2, and array3.

We declare a fourth array to store all the elements of the three arrays and name it totalArray.

We have to do the following :

a) get all the elements from the three arrays ->

we will loop through the first array and store each element in the totalAray. We will do the same for array 2 and 3.

b) now we will sort the totalArray. We will sort the array first before we remove duplicates because it is easier to remove duplicates from a sorted array. Since we aren't allowed to use the in build sorting, we will use the bubble sort algorithm (as it is easier to understand)

c) after bubble sorting, we will get a sorted array. Now we can remove duplicates easily by checking each element in a loop, seeing how many elements are duplicate to it, and pushing the unique elements into a new uniqueArray

d) Now we have to convert each element in the unique and sorted array into lowercase. Since we are not allowed to use the inbuilt toLowerCase function, we will build one on our one using the following logic -> adding 32 to the ASCII value of an upper case element will make it a lowercase element.

e) We want the output as a string. So we append the elements of the finished array into an empty string via a loop.

To make things simple we have handled the integers and characters separately.

Javascript

<script>
getRequiredResult([ 1,2,3, 'd'], [5, 3, 0, 'a'], ['a','d',9]);
getRequiredResult(['w', 'i', 'H', 5], [8, 5, 9, 'c', 'Z'], [6, 4, 'a', 'b', 'y']);

function getRequiredResult(array1, array2, array3) {
   var totalArray = createTotalArray(array1, array2, array3);
   var lowerCaseArray = makeLowercase(totalArray[1]);
   var sortedNumberArray = sortUsingBubbleSort(totalArray[0]);
   var sortedStringArray = sortUsingBubbleSort(lowerCaseArray);
   var uniqueNumberArray = removeDuplicates(sortedNumberArray);
   var uniqueStringArray = removeDuplicates(sortedStringArray);

   var outputString = getOutputString(uniqueNumberArray, uniqueStringArray);


   document.write(outputString);
document.write("<br>");
}

function createTotalArray(array1, array2, array3) {
var totalIntArray = [];
var totalStringArray = [];
  
var len = array1.length;
for (var i = 0; i < len; i++) {
   if (Number.isInteger(array1[i])) {
totalIntArray.push(array1[i]);
} else {
   totalStringArray.push(array1[i]);
}
}
  
len = array2.length;
for (var i = 0; i < len; i++) {
if (Number.isInteger(array2[i])) {
totalIntArray.push(array2[i]);
} else {
   totalStringArray.push(array2[i]);
}
}
  
len = array3.length;
for (var i = 0; i < len; i++) {
if (Number.isInteger(array3[i])) {
totalIntArray.push(array3[i]);
} else {
   totalStringArray.push(array3[i]);
}
}
  
return [
   totalIntArray,
   totalStringArray
];
}

function makeLowercase(array){
var lowercaseArray = [];
len = array.length;
for (var i = 0; i < len; i++) {   
   lowercaseArray.push(convertToLowerCase(array[i]));
}
return lowercaseArray;
}

function convertToLowerCase(str) {
// create a result variable
var result = ''
  
for (var i = 0; i < str.length; i++) {
   // get the code of the current character
   var code = str.charCodeAt(i)
  
// check if it's within the range of capital letters
if (code > 64 && code < 91) {
  
// if so, add a new character to the result string
// of the character from our code, plus 32
result += String.fromCharCode(code + 32)
} else {
  
   // otherwise, just add the current character
result += str.charAt(i)
}
}
  
// return the result
return result
}

function sortUsingBubbleSort(arr){

var len = arr.length,
i, j, stop;

for (i=0; i < len; i++){
for (j=0, stop=len-i; j < stop; j++){
if (arr[j] > arr[j+1]){
swap(arr, j, j+1);
}
}
}

return arr;
}

function swap(arr, first_Index, second_Index){
var temp = arr[first_Index];
arr[first_Index] = arr[second_Index];
arr[second_Index] = temp;
}

function removeDuplicates(sortedArray) {
var len = sortedArray.length - 1;
var newArr = [];

if (len >= 0) {
for (var i = 0; i < len; i++) {
if (sortedArray[i] !== sortedArray[i + 1]) {
newArr.push(sortedArray[i]);
}
}
newArr.push(sortedArray[len]);
}
return newArr;
}

function getOutputString(array1, array2) {
   var outputString= '';
len = array1.length;
for (var i = 0; i < len; i++) {   
   outputString += array1[i];
outputString += ',';
}
  
len = array2.length;
for (var i = 0; i < len; i++) {   
   outputString += array2[i];
outputString += ',';
}
  
return outputString;
}

</script>

Working code output

Please note that Bubble Sort is a simple sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order.

If you found my answer helpful, please provide a thumbs up.

Also please feel free to raise any doubts or issues in the comment section.

Thanks in advance and hope you have a nice day ahead.

Add a comment
Know the answer?
Add Answer to:
Write a javascript function that accepts 3 arrays and returns a string of the elements sorted...
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
  • Write a Javascript function that accepts a string. The function takes the last digit of the...

    Write a Javascript function that accepts a string. The function takes the last digit of the ASCII code and returns a string of digits sorted in ascending order. (without using push, sort or other inbuilt function)

  • Write a function in Java Script that accepts a string. The function takes the last digit...

    Write a function in Java Script that accepts a string. The function takes the last digit of the ASCII code and returns a string of digits sorted in ascending order E.G. function("zDbc") returns “2,8,8,9” (z => 122, D=>68, b=>98, c=>99) ....i am not allowed to use any of the built-in functions

  • C++ Write a function named “hasDescendingDigits” that accepts a string of numbers. It returns true if...

    C++ Write a function named “hasDescendingDigits” that accepts a string of numbers. It returns true if the string contains the digits in descending order. The function can ignore (e.g. skip checking) all the characters that are not digits in the string. Empty string will return false. For example, string of “95421” or “862” or “8622” or “88” or “9” will return true and string of “95423” or “889” or “9445” or “449” or “” will return false.

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

  • ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns...

    ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns a single, merged sorted list as a result. For example, if the two input lists are [0, 2, 4, 6, 8] and [1, 3, 5, 7, 9], the returned result should be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Remember that you can add lists in Python using the + operator ([0] + [1,2,3] = [0,1,2,3]) and can take slices of...

  • 2) Write a function stringManip that takes in a character string and returns a character string...

    2) Write a function stringManip that takes in a character string and returns a character string following these rules: a) any vowel (a, e, i, o, u) is replaced by the character '&' b) any numeric character has 1 added to it (if it was 9, it becomes O instead) (str2num() is useful here) c) all lowercase characters become uppercase d) replace the final character with '!' e) append the length of the string before this step to the end...

  • 1.1. Write a function named "areFirstTwoTheSame AsLast TwoChars" that accepts a string. It returns true if...

    1.1. Write a function named "areFirstTwoTheSame AsLast TwoChars" that accepts a string. It returns true if the first two characters and the last two characters of the string are the same. It returns false otherwise. In addition, if the string is empty or has only one character, it also returns false. For example, these are the strings with their expected return values false falsc "AB" true "ABA" false "ABAB" trus "ABBA" false "ABCABC false "ABCCAB" true 1.2 Write a function...

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

  • 1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector...

    1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector containing the values of the integral (A) for n= 1,2,3,..., n. The function must use the relation (B) and the value of y(1). Your function must preallocate the array that it returns. Use for loop when writing your code. b) Write MATLAB script that uses your function to calculate the values of the integral (A) using the recurrence relation (B), y(n) for n=1,2,... 19...

  • 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