From c427edfdb7cc119e0005560d0fa7d0d0759e06f9 Mon Sep 17 00:00:00 2001 From: Gangaprasad Mohite Date: Thu, 12 Sep 2019 22:31:44 +0530 Subject: [PATCH 1/5] solved array methods practice --- array-methods/practice.js | 274 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 274 insertions(+) diff --git a/array-methods/practice.js b/array-methods/practice.js index b8ec566..cba3cb7 100644 --- a/array-methods/practice.js +++ b/array-methods/practice.js @@ -16,6 +16,16 @@ var pizzas = [ "Spicy Mama", "Margherita" ]; +//for each +pizzas.forEach(function(element) { + console.log(element); +}); +//map +const map1 = pizzas.map(element => element + " spicy"); + console.log(map1); +//filter +const result = pizzas.filter(element => element.length > 9); + console.log(result); var cuts = [ "Chuck", @@ -30,4 +40,268 @@ var cuts = [ "Round" ]; +//for each +cuts.forEach(function(element) { + console.log(element); +}); + +//map +const map1 = cuts.map(element => element + " sweet"); + + +//filter +const result = cuts.filter(element => element.length > 9); + + var numbers = [1, 2, 3, 4, 5, 6, 23, 121, 345, 33, 23, 12, 435, 642, 66, 23]; + +//for each +numbers.forEach(function(element) { + console.log(element); +}); + +//map +const map1 = numbers.map(element => element * 2); + + +//filter +const result = numbers.filter(element => element % 2==0); + + + +var pokemon = [ + "Bulbasaur", + "Ivysaur", + "Venusaur", + "Charmander", + "Charmeleon", + "Charizard", + "Sandshrew" +] +//for each +pokemon.forEach(function(element) { + console.log(element); +}); + +//map +const map1 = pokemon.map(element => element + " fiery"); + +//filter +const result = pokemon.filter(element => element.length > 9); + + + +var bondMovies [ + "Dr. No", + "From Russia with love", + "Goldfinger", + "Thunderball", + "Skyfall", + "Diamonds are Forever" +] + +//for each +bondMovies.forEach(function(element) { + console.log(element); +}); + +//map +const map1 = bondMovies.map(element => element + " awesome"); + + +//filter +const result = bondMovies.filter(element => { + return element.length > 9; +}); + + + var pizzas = [ + "Deep Dish", + "Spicy Mama", + "Peperoni", + "Hawaiian", + "Meatzza", + "Margherita" + ]; +//foreach +pizzas.forEach(function(element){ + alert (element); +}); + +//map +var map1 = pizzas.map(element=> element + " fire and ice"); + +//filter +var result = pizzas.filter (element => element.length>6); + + + + +var numbers = [1, 2, 3, 4, 5, 6, 23, 121, 347, 365, 23, 12, 436, 642, 66, 26]; + +//foreach +numbers.forEach(function(element){ + console.log(element); +}) + +//map +var map1 = numbers.map(element => element + 7) + +//filter +var result = numbers.filter(element => element % 2 == 0) + + +var mcu = [ + "Avengers:Endgame", + "Guardians of Galaxy", + "Iron Man", + "Captain Marvel", + "Thor", + "Captain America", + "Ant Man", + "Spider Man" +] + +//foreach +mcu.forEach(function(element){ + console.log(element); +}) + +//map +var map1 = mcu.map(element => element = " Each of them is awesome") + +//filter +var result = mcu.filter(element => element.length>8) + + +var nolanMovies = [ + "The Dark Knight", + "TENET", + "Interstellar", + "Memento", + "Dunkirk", + "Batman Begins", + "Insomnia" +] + +//foreach +nolanMovies.forEach(function(element){ + console.log(element); +}) + +//map +var map1 = nolanMovies.map(element => element = " Nolan is a genius") + +//filter +var result = nolanMovies.filter(element => element.length>8) + +var rushdie = [ + "The Ground Beneath Her Feet", + "Imaginary Homelands", + "The Golden House", + "Quichotte", + "Midnights Children", + "The Satanic Verses" +] + +//foreach +rushdie.forEach(function(element){ + console.log(element); +}) + +//map +var map1 = rushdie.map(element => element = " You have to read every one") + + +//filter +var result = rushdie.filter(element => element.length>16) + + + +var fibonacci = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946] + +//foreach +fibonacci.forEach(function(element){ + console.log(element); +}) + +//map +var map1 = fibonacci.map(element => element + 2) + +//filter +var result = fibonacci.filter(element => element% 3==0) + +var fruits = [ + "Apple", + "Cherimoya", + "Date", + "Blueberry", + "Blackberry", + "Banana", + "Apricot" +] + +//foreach +fruits.forEach(function(element){ + console.log(element); +}) + +//map +var map1 = fruits.map(element => element + " its so sweet") + +//filter +var result = fruits.filter(element => element.length>16) + +var queenAlbums = [ + "Made in Heaven", + "News of the World", + "Hot Space", + "Sheer Heart Attack", + "Innuendo", + "A Night at the Opera" +] + +//foreach +queenAlbums.forEach(function(element){ + console.log(element); +}) + +//map +var map1 = queenAlbums.map(element => element + " is greatest of all times") + +//filter +var result = queenAlbums.filter(element => element.length>9) + +var numberSeries = [16, 47, 64, 72, 76, 78, 79] + +//foreach +numberSeries.forEach(function(element){ + console.log(element); +}) + +//map +var map1 = numberSeries.map(element => element + 7) + +//filter +var result = numberSeries.filter(element => element % 2 == 0) + + +var radioHead = [ + "Pablo Honey", + "Amnesiac", + "In Rainbows", + "The Bends", + "Kid A", + "OK Computer" +] + +//foreach +radioHead.forEach(function(element){ + console.log(element); +}) + +//map +var map1 = radioHead.map(element => element + " is greatest of all times") + +//filter +var result = radioHead.filter(element => element.length>9) \ No newline at end of file From 30f3db172ac9720a66291fa30bc311771d804a49 Mon Sep 17 00:00:00 2001 From: Gangaprasad Mohite Date: Mon, 16 Sep 2019 10:32:23 +0530 Subject: [PATCH 2/5] Complted 1-array.js and 2-array.js --- array-methods/1-array.js | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/array-methods/1-array.js b/array-methods/1-array.js index e8d84bf..2aaf655 100755 --- a/array-methods/1-array.js +++ b/array-methods/1-array.js @@ -6,34 +6,94 @@ var strings = ["this", "is", "a", "collection", "of", "words"]; // Find largest number in numbers +var maxNum = Math.max(...numbers); +console.log(maxNum); + // Find longest string in strings +var lengthArray = []; +var num = 0; +var longestString = ""; +strings.forEach(function(element) { + if (element.length > num) { + num = element.length; + longestString = element; + } +}); + // Find all the even numbers +var evenArray = numbers.filter(element => element % 2 == 0); +console.log(evenArray); // [12, 4, 18, 6] + // Find all the odd numbers +var oddArray = numbers.filter(element => element % 2); +console.log(oddArray); // [1, 9, 7, 11, 3, 101, 5] + // Find all the words that contain 'is' use string method 'includes' +var containsIs = strings.filter(element => element.includes("is")); +console.log(containsIs); + // Find all the words that contain 'is' use string method 'indexOf' +var arr = []; +var containsIsss = strings.forEach(function(element) { + if (element.indexOf("is") != -1) {arr.push(element);} +}); +arr; // ["this", is] + // Check if all the numbers in numbers array are divisible by three use array method (every) +var divisibleByThree = numbers.every(function(element) {element % 3 == 0}); +divisibleByThree // false + // Sort Array from smallest to largest +var sortStr = strings.sort(); +var numArray = numbers.sort(function (a, b) {return a-b;}); + +sortStr; // ["a", "collection", "is", "of", "swastik"] +numArray; // [1, 3, 4, 5, 6, 7, 9, 11, 12, 18, 101] + // Remove the last word in strings +strings.pop(); +strings // ["this", "is", "a", "collection", "of"] + // Add a new word in the array +strings.push("newWord"); +strings // ["this", "is", "a", "collection", "of", "newWord"] + // Remove the first word in the array +strings.shift(); +strings // ["is", "a", "collection", "of", "newWord"] + // Place a new word at the start of the array use (upshift) +strings.unshift("Hello"); +strings // ["Hello", "is", "a", "collection", "of", "newWord"] + // Make a subset of numbers array [18,9,7,11] +var subSet = numbers.slice(3, 7); +subSet; // [18, 9, 7, 11]; + // Make a subset of strings array ['a','collection'] +var subStr = strings.slice(3, 7); +subStr; // ["a", "collection"] + // Replace 12 & 18 with 1221 and 1881 +numbers.splice(3, 1, 1881); +numbers.splice(1, 1, 1221); + +numbers; // [1, 1221, 4, 1881, 9, 7, 11, 3, 101, 5, 6] + // Replace words with string in strings array // Customers Array @@ -45,6 +105,17 @@ var customers = [ ]; // Find all customers whose firstname starts with 'J' +var startWithJ = customers.filter(element => element.firstname.startsWith('J')); +startWithJ; + // Create new array with firstname and lastname +var nameArray = []; +var name = customers.forEach(function(element) { + nameArray.push(`${element.firstname} + ${element.lastname}`); +}); +nameArray; // ["Joe Blogs", "John Smith", "Dave Jones", "Jack White"] + // Sort the array created above alphabetically + +nameArray.sort(); // ["Dave Jones", "Jack White", "Joe Blogs", "John Smith"] \ No newline at end of file From f7af3df9a4c70f66bc8a637a0d95a1a6fc65ee3e Mon Sep 17 00:00:00 2001 From: Gangaprasad Mohite Date: Mon, 16 Sep 2019 10:37:10 +0530 Subject: [PATCH 3/5] update --- array-methods/2-array.js | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/array-methods/2-array.js b/array-methods/2-array.js index dbfde55..8952a3b 100755 --- a/array-methods/2-array.js +++ b/array-methods/2-array.js @@ -10,6 +10,16 @@ var words = [ //Write a function findLongestWord that takes an array of words and returns the longest one. //If there are 2 with the same length, it should return the first occurrence. +var lengthArray = []; +var num = 0; +var longestString = ""; +words.forEach(function(element) { + if (element.length > num) { + num = element.length; + longestString = element; + } +}); +// This will return the first occurence of longest word. @@ -18,14 +28,24 @@ var numbers1 = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; // Use reduce method of array // Use the above sum and calculate the average. +var reducer = (accumulator, currentValue) => accumulator + currentValue; +var sum1 = numbers1.reduce(reducer); - +var avg1 = sum / numbers1.length; +avg1; // 8.7 var numbers2 = [2, 6, 9, 10, 7, 4, 1, 9]; //Write a function averageNumbers that receives an array of numbers2 and calculate the average of the numbers - +var sum2 = 0; +function averageNumbers(numArray) { + numArray.forEach(function(element) { + sum2 += element; + }); + return sum2 / numArray.length; +} +averageNumbers(numbers2); // 48 / 8 = 6. var words2 = [ 'seat', @@ -41,5 +61,12 @@ var words2 = [ ]; //Write a function averageWordLength that receives an array of words2 and calculate the average length of the words. - - +var word2len = 0; +function avgWords(words2) { + words2.forEach(function(element) { + word2len += element.length; + }); + var wordAvg = word2len / words2.length; + return wordAvg; +} +avgWords(words2); // 5.3 \ No newline at end of file From a7b08f82b3c66ff9a06713f7153e697300fd8f90 Mon Sep 17 00:00:00 2001 From: Gangaprasad Mohite Date: Mon, 16 Sep 2019 21:23:26 +0530 Subject: [PATCH 4/5] array methods --- array-methods/3-array.js | 54 +++++++++++++++++++++++++++++++++++++++ array-methods/4-array.js | 25 ++++++++++++++++++ array-methods/5-object.js | 16 +++++++++--- string-methods/01.md | 29 +++++++++++++++++++++ 4 files changed, 121 insertions(+), 3 deletions(-) diff --git a/array-methods/3-array.js b/array-methods/3-array.js index 15e6b94..f0ae4f9 100755 --- a/array-methods/3-array.js +++ b/array-methods/3-array.js @@ -15,6 +15,11 @@ var words = [ // Write a function uniqueArray that receives an array of words as a parameter. And remove the duplicates, and return a new array. // (indexOf) +function uniqueArray(words) { + var wordsFilter = words.filter((element, index) => words.indexOf(element) == index); + return wordsFilter; +} + var words2 = [ @@ -30,6 +35,11 @@ var words2 = [ // Write a function doesWordExist that will take in an array of words as one argument, and a word to search for as the other. Return true if it exists, otherwise, return false. Don't use indexOf for this one. +function doesWordExist(words2, searchItem) { + return words2.includes(searchItem); +} + +doesWordExist(words2, 'machine'); // true; @@ -50,7 +60,19 @@ var words3 = [ // Write a function howManyTimes that will take in an array of words as one argument, and a word to search for as the other. The function will return the number of times that word appears in the array. +function howManyTimes(words3, searchItem) { + var arr = [searchItem]; + words3.forEach(element => { + if(arr.includes(element)) { + arr.push(element); + } + }); + return arr.length - 1; +} +howManyTimes(words3, 'matter'); // 4 +howManyTimes(words3, 'machine'); // 1 +howManyTimes(words3, 'matter-machine'); // 0 @@ -74,6 +96,14 @@ let data = [ } ] +data.reduce((acc, value) => { + if (value.country == 'China') { + return acc; + } else { + return acc + value.pop; + } +}, 0); + // Use reduce method and summorize the collection like // { banana: 2, cherry: 3, orange: 3, apple: 2, fig: 1 } @@ -91,6 +121,30 @@ const fruitBasket = [ 'fig' ]; +// var finalObject = {}; + +// fruitBasket.reduce((acc, value) => { +// if (finalObject[acc] == 0) { +// finalObject[acc]++; +// } +// if (!finalObject[value]) { +// finalObject[value] = 0; +// } + +// finalObject[value]++; +// }, 0); + +// finalObject; + +fruitBasket.reduce((acc, value) => { + if (acc[value]) { + acc[value]++; + } else { + acc[value] = 1; + } + return acc; +}, {}); + // Bonus Question (Solve only if you have time) diff --git a/array-methods/4-array.js b/array-methods/4-array.js index 983741b..4954ebf 100644 --- a/array-methods/4-array.js +++ b/array-methods/4-array.js @@ -31,11 +31,36 @@ var data = [ // your code goes here +var sum = 0; +function sumDogAge() { + for (var i = 0; i < data.length; i++) { + if (data[i].type == 'dog') { + sum += data[i].age * 7; + } + } +} + +sumDogAge(); // 105 + // Solution is 105 + // Write the same function using // 1. filter - for filtering the cat or dog // 2. map - to multiply human year to dog year // 3. reduce - to accumulate total age. +// Solution 105 + +function sumDogAge() { + var typeDog = data.filter(element => element.type == 'dog'); + var dogYear = typeDog.map(element => element.age * 7); + var sum = dogYear.reduce((x, y) => x + y); + + return sum; +} + +sumDogAge(); + + // Solution 105 diff --git a/array-methods/5-object.js b/array-methods/5-object.js index db7fcec..93cabfd 100644 --- a/array-methods/5-object.js +++ b/array-methods/5-object.js @@ -1,6 +1,8 @@ // 1. Write a JavaScript program to list the properties and values of a JavaScript object. (Object.keys) - +function listKeyValues(obj) { + Object.entries(obj); +} // 2. Write a JavaScript program to delete the rollno property from the following object. Also print the object before or after deleting the property. var student = { @@ -9,7 +11,15 @@ var student = { rollno : 12 }; +function deleteRollNo (obj) { + delete obj.rollno; + console.log(obj); +} +deleteRollNo(student); - -// 3. Write a function to get the length of an object. \ No newline at end of file +// 3. Write a function to get the length of an object. +function objLen(obj) { + return Object.keys(obj).length; +} +objLen(student); \ No newline at end of file diff --git a/string-methods/01.md b/string-methods/01.md index 6dc688e..e6d1f6f 100644 --- a/string-methods/01.md +++ b/string-methods/01.md @@ -6,6 +6,10 @@ Write a JavaScript function to check whether an `input` is a *string or not*. Example: ```js // your code goes here + function is_string(input) { + return typeof(input) == "string" ? true : false; + } + console.log(is_string('tony stark')); true console.log(is_string([1, 2, 4, 0])); @@ -18,6 +22,10 @@ Write a JavaScript function to check whether a string *is blank or not*. Example: ```js // your code goes here + function is_Blank(str) { + return str.length == 0 ? true : false; + } + console.log(is_Blank('')); true console.log(is_Blank('abc')); @@ -30,6 +38,9 @@ Write a JavaScript *function to split a string and convert it into an array* of Example: ```js // your code goes here + function string_to_array(str) { + return str.split(" "); + } console.log(string_to_array("Robin Singh")); ["Robin", "Singh"] ``` @@ -40,6 +51,9 @@ Write a JavaScript function to remove specified number of characters from a stri Example: ```js // your code goes here + function truncate_string(str, index) { + return str.substring(0, index); + } console.log(truncate_string("Robin Singh",4)); "Robi" ``` @@ -50,6 +64,12 @@ Write a JavaScript function *to convert* a string in *abbreviated form*. Example: ```js // your code goes here + function abbrev_name(name) { + name = name.split(" "); + name[1] = name[1][0] + "."; + + return name.join(" "); + } console.log(abbrev_name("Robin Singh")); "Robin S." ``` @@ -61,6 +81,12 @@ Write a JavaScript function to hide email addresses to protect from unauthorized Example: ```js // your code goes here + var protect_email = (mail) => { + var i = mail.indexOf("_"); + var j = mail.indexOf("@"); + + return mail.replace(mail.slice(i, j), "..."); + } console.log(protect_email("robin_singh@example.com")); "robin...@example.com" ``` @@ -71,6 +97,9 @@ Write a JavaScript function to *parameterize a string*. Example: ```js // your code goes here + function string_parameterize(str) { + return str.split(" ").join("-").toString(); + }; console.log(string_parameterize("Robin Singh from USA.")); "robin-singh-from-usa" ``` From aaa57ca007892d7d002b4a7fcced1db1bc467705 Mon Sep 17 00:00:00 2001 From: Gangaprasad Mohite Date: Mon, 16 Sep 2019 21:40:23 +0530 Subject: [PATCH 5/5] solved string and array methods --- string-methods/01.md | 132 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 128 insertions(+), 4 deletions(-) diff --git a/string-methods/01.md b/string-methods/01.md index e6d1f6f..34ee364 100644 --- a/string-methods/01.md +++ b/string-methods/01.md @@ -110,6 +110,9 @@ Write a JavaScript function to *capitalize the first letter of a string*. Example: ```js // your code goes here + function capitalize(str) { + return str.replace(str[0], str[0].toUpperCase()); + } console.log(capitalize('js string exercises')); "Js string exercises" ``` @@ -120,16 +123,28 @@ Write a JavaScript function to *capitalize* the first letter *of each word* in a Example: ```js // your code goes here + function capitalize_Words(str) { + str = str.split(" "); + + for (let i = 0; i < str.length; i++) { + str[i] = capitalize(str[i]); + } + + return str.join(" "); + + }; + console.log(capitalize_Words('js string exercises')); "Js String Exercises" ``` -### swapcase +### swapcase Write a JavaScript function that takes a string which has lower and upper case letters as a parameter and *converts upper case letters to lower case*, and lower case letters to upper case. Example: ```js // your code goes here + let swapcase = (str) => str.split('').map( (char) => (char >= 'a' && char <= 'z') ? char.toUpperCase() : char.toLowerCase()).join(''); console.log(swapcase('AaBbc')); "aAbBC" ``` @@ -140,12 +155,20 @@ Write a JavaScript function *to convert a string into camel case*. Example: ```js // your code goes here + var camelize = (str) => { + str = str.split(" "); + capitalize_Words(str); + str = str.join(""); + + return str; + } + console.log(camelize("JavaScript Exercises")); "JavaScriptExercises" console.log(camelize("JavaScript exercises")); "JavaScriptExercises" console.log(camelize("JavaScriptExercises")); - "JavaScriptExercises" + "J avaScriptExercises" ``` ### uncamelize @@ -153,6 +176,19 @@ Write a JavaScript function to *uncamelize* a string. Example: ```js // your code goes here + let uncamelize = (str, delimeter) => { + let res = ''; + + for (let i = 0; i < str.length; ++i) { + if(str[i] >= 'a' && str[i] <= 'z') res += str[i]; + + if (str[i] >= 'A' && str[i] <= 'Z') { + res += delimeter; + res += str[i].toLowerCase(); + } + } + return res; + } console.log(uncamelize('helloWorld','_')); "hello_world" ``` @@ -162,6 +198,15 @@ Write a JavaScript function to *concatenates a given string n times* (default is Example: ```js // your code goes here + var repeat = (str, num) => { + var result = ""; + while(num > 0) { + result += str; + num--; + } + return result; + } + console.log(repeat('Ha!',3)); "Ha!Ha!Ha!" ``` @@ -171,6 +216,10 @@ Write a JavaScript function to insert a string within a string at a *particular Example: ```js // your code goes here + var insert = (str, word, index) => { + return str.substring(0, index) + word + str.substring(index); + } + console.log(insert('We are doing some exercises.','JavaScript ',18)); "We are doing some JavaScript exercises." ``` @@ -180,6 +229,22 @@ Write a JavaScript function to humanized number (Formats a number to a human-rea Example: ```js // your code goes here + let humanize_format = (num) => { + num = String(num); + + let last = num[num.length - 1]; + + switch (last) { + case '1': + return num + 'st'; + case '2': + return num + 'nd'; + case '3': + return num + 'rd'; + default: + return num + 'th'; + } + } console.log(humanize_format(301)); console.log(humanize_format(402)); "301st" @@ -191,6 +256,10 @@ Write a JavaScript function to *truncates a string if it is longer than the spec Example: ```js // your code goes here + var text_truncate = (str, len, char="...") => { + var substr = str.substring(len+1, str.length); + return str.replace(substr, char); + } console.log(text_truncate('We are doing JS string exercises.',15,'!!')) "We are doing !!" ``` @@ -200,6 +269,13 @@ Write a JavaScript function *to chop a string into chunks of a given length*. Example: ```js // your code goes here + var string_chop = (str, len) => { + var chunks = []; + for (var i = 0; i < str.length; i += len) { + chunks.push(str.substring(i, i+len)); + } + return chunks; + } console.log(string_chop('w3resource',2)); ["w3", "re", "so", "ur", "ce"] ``` @@ -209,6 +285,15 @@ Write a JavaScript function to *count the occurrence of a substring* in a string Example: ```js // your code goes here + function count(str, word) { + var arr = [word]; + str = str.split(" "); + for (var i = 0; i < str.length; i++) { + if (str[i].toLowerCase() == arr[0].toLowerCase()) {arr.push(str[i]);}; + } + + return arr.length - 1; + } console.log(count("The quick brown fox jumps over the lazy dog", 'the')); 2 ``` @@ -216,24 +301,63 @@ Example: ### Write a JavaScript function to chop a string into chunks of a given length. Input ( String, Number) -> ('Hello World!', 2) Output ['He', 'll', 'o ', 'Wo', 'rl', 'd!'] - +```js +var string_chop = (str, len) => { + var chunks = []; + for (var i = 0; i < str.length; i += len) { + chunks.push(str.substring(i, i+len)); +} + return chunks; +} +``` ### Write a JavaScript function to count the occurrence of a substring in a string. Input (String, String) -> ('The world of the dogs', 'the') Output (Number) -> 2 +```js +function count(str, word) { + var arr = [word]; + str = str.split(" "); + for (var i = 0; i < str.length; i++) { + if (str[i].toLowerCase() == arr[0].toLowerCase()) {arr.push(str[i]);}; +} + return arr.length - 1; +} +``` ### Write a JavaScript function to strip leading and trailing spaces from a string. Input (String) -> ('Hello World ') Output String -> "Hello World" +```js +var stripSpaces = (str) => { + return str.trim(); +} +``` ### Write a JavaScript function to truncate a string to a certain number of words. Input (String, Number) -> ('The quick brown fox jumps over the lazy dog', 4) Output ( String ) -> "The quick brown fox" - +```js +var truncate = (str) => { + str = str.split(" "); + return str.slice(0, 4).join(" "); +} +``` ### Write a JavaScript function to alphabetize a given string.(A - z) Input (String) -> 'United States' Output 'SUadeeinsttt' +```js +var alphabetize = (str) => { + str = str.split(""); + return str.sort().join(""); +} +``` ### Write a JavaScript function to test case insensitive (except special Unicode characters) string comparison. Input ( String String) -> ('abcd', 'AbcD') Output Boolean -> true ('ABCD', 'Abce') -> false +```js +var caseInsensitive = (str1, str2) => { + return (str1.toLowerCase() == str2.toLowerCase()); +} +``` \ No newline at end of file