Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array? - javascript

This is my code so far; All I've done is loop through each letter.
I'm stuck on how to test whether or not the strings have the same characters.
function mutation(arr) {
for (var i=0;i<arr.length;i++){
for(var j =0;j<arr[i].length;j++){
}
}
}
mutation(['hello', 'hey']);
The characters don't have to be at the same index, so for example, ['Alien', 'line'], should return true.

I used the answer above but modified it to remove the case, otherwise i was getting a false on Hello and hello
function mutation(arr) {
//first split the arr into the two inputs and convert all to lower case
var firstArray = arr[0].toLowerCase().split("");
var secondArray = arr[1].toLowerCase().split("");
//now using the code provided by the above comment which is really clean
var count = 0;
for (var i =0; i < secondArray.length; i++) {
if(firstArray.indexOf(secondArray[i]) > -1 ) {
count++;
}
}
if (count == secondArray.length) {
return true
}
//changed the code provided above to handle the true/false criteria of the excercise
else {return false; }
}
mutation(['hello', 'hey']);

You only have to loop once, and compare the second array against the first.
function mutation(arr) {
var arr1 = arr[0].split('');
var arr2 = arr[1].split('');
var count = 0;
for (var i =0; i < arr2.length; i++) {
if(arr1.indexOf(arr2[i]) > -1 ) {
count++;
}
}
if (count == arr2.length) {
console.log('all in');
}
}
mutation(['alien', 'line']);
Or you could use filter:
function mutation(arr) {
var arr1 = arr[0].split('');
var arr2 = arr[1].split('');
if (arr2.filter(function(element, index) { return arr1.indexOf(element); }).length === arr2.length) {
console.log('all in');
}
}
mutation(['alien', 'line']);

The counter in the accepted answer is unnecessary.
function mutation(arr) {
var arr1 = arr[0].toLowerCase().split('');
var arr2 = arr[1].toLowerCase().split('');
for (var i=0; i < arr2.length; i++) {
if(arr1.indexOf(arr2[i]) == -1 ) {
return false;
}
}
return true;
}
mutation(["hello", "hey"]);

Clean, modern and easy to read:
function mutation(arr) {
const firstEl = arr[0].toLocaleLowerCase();
const secondEl = arr[1].toLocaleLowerCase().split('');
return secondEl.every(el => firstEl.includes(el));
}
console.log('mutation(["hello", "hey"]): ', mutation(["hello", "hey"]));
console.log('mutation(["Alien", "line"]): ', mutation(["Alien", "line"]));

How about something cleaner? Just a little modification to an above code.
function mutation(arr) {
var first = arr[0].toLowerCase().split('');
var second = arr[1].toLowerCase().split('');
var count = 0;
// Check every character and if the index is found add one
for (var s in second){
if (first.indexOf(second[s]) > -1) {
count+= 0;
} else
count++;
}
if (count === 0)
return true;
else
return false;
}

even lesser code for this problem
function mutation(arr) {
var arr1 = arr[0].toLowerCase().split('');
var arr2 = arr[1].toLowerCase().split('');
for(var i of arr2)
if(arr1.indexOf(i)===-1)
return false;
return true;
}
mutation(["hello", "hey"]);

the code above will not work in all cases
because if the first word in array is shorter than second one you need second for loop
for (var i=0; i<arr1.length; i++){
if (arr2.indexOf(arr1[i])==-1){
return false;
you need 2 for loops
function mutation(arr) {
var arr1=arr[0].toLowerCase().split("");
var arr2=arr[1].toLowerCase().split("");
if (arr1.length<arr2.length ){
for (var i=0; i<arr1.length; i++){
if (arr2.indexOf(arr1[i])==-1){
return false;
}
}
}
else if (arr1.length>=arr2.length ){
for (var j=0; j<arr2.length; j++){
if (arr1.indexOf(arr2[j])==-1){
return false;
}
}
}
return true;
}
//mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]);
mutation([ "qrstu", "zyxwvutsrqponmlkjihgfedcba"]);
enter code here

Related

JSON.stringify not working when comparing two arrays

I am trying to compare two arrays and check if they are the same. According to the logic below the function should return DRAW as the arrays I am comparing to are exactly the same. It returns undefined instead. I cannot figure it out why it doesn't return draw. If I change the && to || it returns draw so I assume there is an issue with the logical operator.
function isSolved(board) {
// TODO: Check if the board is solved!
var arraya = [1,1,1];
var arrayb = [2,2,2];
for (var i = 0; i < board.length; i++){
for (var j = 0; j < board[i].length; j++) {
if (board[i][j] === 0) {
return false;
} else if (
(JSON.stringify(board[i]) === JSON.stringify(arraya))
&& (JSON.stringify(board[i]) ===JSON.stringify(arrayb))) {
return "draw"
}
}
}
}
console.log(
isSolved([
[1,1,1],
[1,2,2],
[2,2,2]
])
);
You can use JSON.stringify like this:
var arraya = [1,1,1];
var arrayb = [2,2,2];
function isSolved(a, b) {
return JSON.stringify(a) === JSON.stringify(b) ? "draw" : false;
};
var input = [[1,1,1], [1,2,2], [2,2,2]];
for (var item of input) {
console.log(isSolved(item, arraya));
console.log(isSolved(item, arrayb));
console.log("=====");
}

I'm only able to return 1 array

I'm trying to take this array and split it into 2 new arrays, evens and odds and return them. When I run the code below I am only getting the odds, why is that? And what can I do to solve it?
Thanks in advance.
var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13];
function divider( arr ) {
var evens = [];
var odds = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 0) {
evens.push(arr[i]);
} else {
odds.push(arr[i]);
}
}
return(evens, odds);
}
divider(numbersArray);
Because JavaScript can only return one value. Ever.
return(evens, odds)
evaluates to the same value as
return odds
due to the comma operator wrapped in grouping parenthesis.
Perhaps returning an array of arrays (or even an object of arrays) is useful..
return [evens, odds]
You should return your results as an array.
return [evens, odds];
And then to access the results:
var evens;
var odds;
var arrayResults = divider(numbersArray);
evens = arrayResults[0];
odds = arrayResults[1];
console.log(evens);
console.log(odds);
In Javascript, you can only return ONE value. So, if you want to return multiples values, to separate them, you can put them in an array or in an object :
return([evens, odds]);
OR
return({evens: evens, odds: odds})
The result of evaluating (evens, odds) is odds, that is returned thus.
This is how comma operator works.
Use the following statement instead:
return { 'evens': evens, 'odds': odds };
As an example:
var v = divider(numberArrays);
v.evens; // get evens this way
v.odds; // get odds this way
You can return only one entity from a function. Its better to wrap your results in single object.
var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13];
function divider( arr ) {
var evens = [];
var odds = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 0) {
evens.push(arr[i]);
} else {
odds.push(arr[i]);
}
}
return {evens:evens, odds:odds};
}
divider(numbersArray);
Es5 doesn't support tuples, You should wrap your return
in an object like here
var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13];
function divider( arr ) {
var evens = [];
var odds = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 0) {
evens.push(arr[i]);
} else {
odds.push(arr[i]);
}
}
return {evens:evens,
odds:odds};
}
divider(numbersArray);
Or in an array as the other aswers show
You could return an object, like this:
var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13];
function divider( arr ) {
var evens = [];
var odds = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 0) {
evens.push(arr[i]);
} else {
odds.push(arr[i]);
}
}
return {evens, odds};
}
divider(numbersArray);

removing multiple arrays from a 2D array

My web app is taking in arbitrarily large 2D arrays that sometimes look something like this:
var multiArray = [["","","",""],[1,2,3],["hello","dog","cat"],["","","",""]];
I want to write a function to take out every array inside of multiArray that is comprised entirely of quotes. In other words, any array that looks like this:
["","","",""]
should be deleted from multiArray.
I tried writing the following function, but the problem with using splice in a for loop is that splicing will change the length of the array, and I end up trying to access undefined elements. Please help!
Thanks!
Here's the incorrect function I wrote:
function cleanWhitespace(arrayOfArrays) {
var i;
var arrayOfArraysLength = arrayOfArrays.length;
for (i = 0; i < arrayOfArraysLength; i++) {
var cleanedArray = $.grep(arrayOfArrays[i], function(element) {
return element != ""
});
if (cleanedArray.length == 0) {
arrayOfArrays.splice(i, 1);
}
}
return arrayOfArrays;
};
You can use $.grep :
multiArray = $.grep(multiArray, function(v){
return v.join('');
});
Fiddle : http://jsfiddle.net/scZcB/
on the fly:
var multiArray = [["","","",""],[1,2,3],["hello","dog","cat"],["","","",""]];
var outputArr = removeQuoteArrays(multiArray);
console.log(outputArr);
function removeQuoteArrays(arr) {
var outputArr = [];
for (var i = 0; i < arr.length; i++) {
var currArr = arr[i];
var isAllQuotes = true;
for (var j = 0; j < currArr.length; j++) {
if (currArr[j] != "") {
isAllQuotes = false;
break;
}
}
if (!isAllQuotes) {
outputArr.push(currArr);
}
}
return outputArr;
}
Here's a JSFiddle.
Create a new array instead.
// Only add if...
cleanedArray = multiArray.filter(function(arr){
// Some elements are not blank
return arr.some(function(e){ return e !== "" })
})
I added a length check to your function to break out of the loop if the index reaches the array length:
if (i >= arrayOfArrays.length)
break;
Which makes:
function cleanWhitespace(arrayOfArrays) {
var i;
var arrayOfArraysLength = arrayOfArrays.length;
for (i = 0; i < arrayOfArraysLength; i++) {
var cleanedArray = $.grep(arrayOfArrays[i], function(element) {
return element != ""
});
if (cleanedArray.length == 0) {
arrayOfArrays.splice(i, 1);
if (i >= arrayOfArrays.length)
break;
}
}
return arrayOfArrays;
};
var multiArray = [["","","",""],[1,2,3],["hello","dog","cat"],["","","",""]];
function cleanWhitespace(arrayOfArrays) {
for (var i = 0; i < arrayOfArrays.length; i++) {
var emptyElements = 0;
for (var j = 0; j < arrayOfArrays[i].length; j++ ) {
if (arrayOfArrays[i][j] === "") {
emptyElements++;
}
}
if (emptyElements === arrayOfArrays[i].length) {
arrayOfArrays.splice(i, 1);
}
}
return arrayOfArrays;
}
console.log(cleanWhitespace(multiArray));
http://jsfiddle.net/4Jfr9/

Checking if a string contains any part of an array element

I just started learning javascript and I'm working on small chrome extension that checks a certain website's item listings for any items
that contain keywords provided by the user. I'm looking for some help on a method to compare a string to an array of strings.
So I have these variables:
var itemDetails = '[alter] saber 1/8 figure unopened';
var trackingList = ['alter figure', 'magic', 'sword art'];
I need to check if itemDetails contains any of the strings in trackingList. I know I can use indexOf() like this:
function checkArray(str, arr){
for(var i=0; i < arr.length; i++){
if(str.indexOf(arr[i]) > -1)
return true;
}
return false;
}
checkArray(itemDetails,trackingList); // returns false
However, for strings with multiple words in them, such as 'alter figure', I want checkArray() to return true as long as both of the words in the string appear anywhere in itemDetails. So checkArray() should return true in the example above since both 'alter' and 'figure' are in itemDetails.
The method I'm currently using is to split each element in trackingList:
function splitTrackList(arr){
var newarr = [];
for(var i = 0; i < arr.length; i++){
newarr[i] = arr[i].split(" ");
}
return newarr;
}
trackList = splitTrackList(trackList);
// [['alter', 'figure'], ['magic'], ['sword', 'art']]
And then compare using indexOf():
function newCheckArray(str, arr){
var passed = true;
for(var i=0; i < arr.length; i++){
for(var j=0; j < arr[i].length; j++){
if(str.indexOf(arr[i][j]) == -1){
passed = false;
break;
}
else passed = true;
}
if(passed) //stop loop if match found
break;
}
return passed;
}
newCheckArray(itemDetails,trackingList); //returns true
My method works so far but I'm guessing there is a much faster/efficient way of doing this. Please let me know what you think. Thank you in advance.
I would do something like
https://jsfiddle.net/denov/FXjXq/3/
var arr = [
"cat dog mouse",
"blue yellow green",
"pizza burrito hamburger"
];
function isInString(needle, haystack) {
return new RegExp('\\b' + needle + '\\b').test(haystack);
}
function checkForStringInArray(stringToFind, arr) {
var inString = false;
for(var i=0; i < arr.length; i++){
inString = isInString(stringToFind, arr[i]);
if(inString) break;
}
return inString;
}
var inString = checkForStringInArray('yellow', arr);
You can use regular expressions. I am giving you an example, although it is not the most efficient
function checkArray(str, arr){
for(var i=0; i < arr.length; i++){
if(str.match((".*" + arr[i].trim() + ".*").replace(" ", ".*")))
return true;
}
return false;
}
Here I alter the string "key1 keyn" to ".*key1.keyn." so it could match those keys everywhere in the string. Have in mind that this will match them if they are part of another word. Good luck.
P.S. Learn regular expressions. They are pretty important in almost every language.
This should work :
function checkArray(str, arr){
for (var j = 0; j < arr.length;j++) {
wordtab = splitTrackList(arr[j]);
for(var i=0; i < tab.length; i++){
if(str.indexOf(wordtab[i]) > -1)
return true;
}
}
return false;
}
Here is a solution with lambda expressions:
var itemDetails = '[alter] saber 1/8 figure unopened';
var trackingList = ['alter saber', 'magic', 'sword art'];
var trackingList2 = ['alter was', 'magic', 'sword art'];
if(trackingList.map(str => str.split(" ")).filter(arrtemp => arrtemp.filter(strin => itemDetails.indexOf(strin) > -1).length == arrtemp.length).length > 0) {
console.debug("true");
} else {
console.debug("false")
}
if(trackingList2.map(str => str.split(" ")).filter(arrtemp => arrtemp.filter(strin => itemDetails.indexOf(strin) > -1).length == arrtemp.length).length > 0) {
console.debug("true");
} else {
console.debug("false")
}
https://jsfiddle.net/wrz1m0b5/
The regex solution only works if words are in correct order.

how to remove the repeated value in an array and in between arrays using jQuery

In my code I need to find the repeated value and give an alert using jQuery. Below is the example arrays and my requirement. Please help.
a1 = {1,2,3,4,5,6,4}
a2= {9,8,7},
a3= {a,b,c,d,e,7}
In the above arrays I need to get the value 4 and give alert because it is repeating in array "a1" and I need to get the value 7 because it is repeating in the arrays 'a2' and 'a3'.
The first issue I fixed like as follows. Ineed to to fix the second one.
for (var $i = 0; $i<= $last; $i++)
{
var hours = [];
var minutes = [];
var activeTime = [];
$.each($('.hour'+$i),function() {
hours.push($(this).val());
});
$.each($('.hour'+$i).next('select'),function(){
minutes.push($(this).val());
});
for ( var i = 0; i < hours.length; i++ ) {
activeTime.push(hours[ i ]+":"+minutes[ i ]+":"+"00");
}
for ( var i = 0; i <= hours.length; i++ ) {
if ( hours[ i ] === "" )
{
$("#timeValidate"+$i).css("display", "block");
return false;
}
else
{
$("#timeValidate"+$i).css("display", "none");
}
}
for(var i=0; i< activeTime.length; i++)
{
for(var j = 0; j< activeTime.length; j++)
{
if( i != j)
{
if(activeTime[j] == activeTime[i])
{
$("#timeValidate"+$i).text("");
$("#timeValidate"+$i).text("active time"+activeTime[j]+" is repeating");
$("#timeValidate"+$i).css("display", "block");
return false;
}
}
}
}
}
function getDuplicatesFromArrays() {
var allItems = Array.prototype.concat.apply([], arguments);
var duplicates = [];
var hash = {};
allItems.forEach(function(x) {
if(hash.hasOwnProperty(x)) duplicates.push(x);
hash[x] = 1;
});
return duplicates;
}
The above function accepts any number of arrays, and yields the duplicates:
getDuplicatesFromArrays(a1,a2,a3) // [4, 7]
Demo
It works as sort of an inverse hash sieve; adding to duplicates only if the item was already in the hash.
Try sugar.js, it has advanced functions for arrays (and others). For example, you can use a combination of unique() and subtract() to get an array that contains only the elements that are repeated. Then you can parse it and alert for each one.
Here you go:
var original = [].concat(a,b,c),
dupes = [];
for (var c = 0; c < original.length; c++) {
if (original.filter(function(v) {
return v == original[c];
}).length > 1) {
dupes.push(original[c]);
}
}
alert($.grep(dupes, function(v, k){return $.inArray(v , dupes) === k;}));
Here is a demo: http://jsfiddle.net/q2c42/

Categories