This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 2 years ago.
First let me say am a novice at Javascript so this may be more obvious than I think. But what I'm trying to do is create a new Javascript array from two existing arrays. My thinking is to create a for loop, test if they are equal and if not push that number into a new array. For example.
var allArr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
var myArr = [7,8,9,10,14,17];
var fnlArr = [];
What I would like to end up with is fnlArr = [1,2,3,4,5,6,11,12,13,15,16,18,19,20];
Something like:
for (n = 0; n < myArr.length; n++){
for (p = 0; p < allArr.length; p++){
if (p!==myArr[n]){
fnlArr.push(p);}
}
}
I was also thinking maybe to grep to see if they are the same???
Any help would be appreciated.
You can use .filter:
const allArr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
const myArr = [7,8,9,10,14,17];
const set = new Set(myArr);
const fnlArr = allArr.filter(e => !set.has(e));
console.log(fnlArr);
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 4 years ago.
I have a basic problem with for loop "tostring" iteration.
Description:
There are 12 <input type="text"> containers.
I want to detect when user writes si word in any 3 out of 12 containers.
If user types si exactly 3 times I set an alert called You made it!.
Problem:
Most likely that line is incorrect: if(cube[i].includes("si")).
Not sure how to check all containers in for loop just to count the number of si word. (adding number to string in for loop to call another variables e.g. cube1, cube2, cube3 etc.)
Any help would be appreciated, thank you :)
function transmute() {
var cube1 = document.getElementById("cube_slot1").value;
var cube2 = document.getElementById("cube_slot2").value;
var cube3 = document.getElementById("cube_slot3").value;
var cube4 = document.getElementById("cube_slot4").value;
var cube5 = document.getElementById("cube_slot5").value;
var cube6 = document.getElementById("cube_slot6").value;
var cube7 = document.getElementById("cube_slot7").value;
var cube8 = document.getElementById("cube_slot8").value;
var cube9 = document.getElementById("cube_slot9").value;
var cube10 = document.getElementById("cube_slot10").value;
var cube11 = document.getElementById("cube_slot11").value;
var cube12 = document.getElementById("cube_slot12").value;
var counter = 0;
for (var i = 1; i <= 12; i++) {
if (cube[i].includes("si")) {
counter += 1;
}
}
if (counter == 3) {
alert("You made it!");
}
}
Perhaps you could simplify your approach, but iterating over your elements within a for loop (via a dynamic id), and counting occasions where the si substring is found in input values.
If three or more cases are encountered, display the alert() and then break early from the loop:
function transmute() {
for(var i = 1; i <= 12; i++) {
var id = 'cube_slot' + i;
var value = document.getElementById(id).value;
if(value.includes('si')) {
counter += 1;
}
if(counter >= 3) {
alert("You made it!");
break
}
}
}
This question already has answers here:
Recursively print all permutations of a string (Javascript)
(11 answers)
Closed 6 years ago.
I need to write a JavaScript function that shows me all the permutations of the digits of the number passed as an argument, but not sure how to do it.
For example, given 1020, it would produce
1020 , 0120 , 0210 , 0102.
Here's a working solution. Hope it helps!
var result = [];
var newArray = [];
function permute(someArray) {
var i, ch;
for (i = 0; i < someArray.length; i++) {
ch = someArray.splice(i, 1)[0];
newArray.push(ch);
if (someArray.length == 0) {
result.push(newArray.slice());
}
permute(someArray);
someArray.splice(i, 0, ch);
newArray.pop();
}
return result;
};
var n = 1901;
var arr = (""+n).split("");
var myResult = permute(arr);
for(var i in myResult){
console.log(myResult[i].join(""));
}
This question already has answers here:
Get HTML5 localStorage keys
(15 answers)
Closed 7 years ago.
I'm trying to print the names of all the keys stored in localStorage with each key in a separate line. This is my code:
function viewsaved(){
$('#saved').show();
var stuffsaved = Object.keys(localStorage);
var splitit = stuffsaved.split(',');
for (var i = 0 ; i < splitit.length ; i++ ){
$('#saved').append(splitit[i]+"<br>");
}
}
when I call the function, it does nothing.
How do you do this properly?
Object.keys returns an array, not a string. Just modify slightly:
var stuffsaved = Object.keys(localStorage);
for (var i = 0 ; i < stuffsaved.length ; i++ ) {
$('#saved').append(stuffsaved[i]+"<br>");
}
If you have or expect a lot of keys, I would suggest building the list in a temporary variable first to avoid frequent DOM update, for example:
var keys = Object.keys(localStorage);
var list = "";
for (var i = 0 ; i < keys.length ; i++ ) {
list += keys[i] + "<br>";
}
$('#saved').append(list);
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 7 years ago.
I want to know why this procedure doesn't replace words
I have to do a procedure which reads a string and replace all word like this {{employee.Name}} into a value on the ticket's scope
var mySplitResult = Val.split(' ');
for (var i = 0; i < mySplitResult.length; i++) {
if (mySplitResult[i].match("{{") && mySplitResult[i].match(".")) {
var start = mySplitResult[i].lastIndexOf(".") + 1;
var end = mySplitResult[i].indexOf("}}");
var result = mySplitResult[i].substring(start, end);
for (var key in ticket.PNData) {
if (key == result) {
change.replace(mySplitResult[i], ticket.PNData[key]);
alert(change)
}
}
}
}
In JavaScript strings are immutable which means you must assign the result to a variable.
mySplitResult[i] = mychange.replace(mySplitResult[i], ticket.PNData[key]);