Skip to content

Instantly share code, notes, and snippets.

@fitsum
Last active April 26, 2024 05:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitsum/9eef9b53de9dfa3c3cd785c9614401f6 to your computer and use it in GitHub Desktop.
Save fitsum/9eef9b53de9dfa3c3cd785c9614401f6 to your computer and use it in GitHub Desktop.
replaces array items in pairs
console.clear()
let used = [],
replaced = []
/*
TODO: only needed for early testing. can del
len = 20,
createArr = function(N) {
return Array.apply(null, {
length: N
}).map(Number.call, Number)
} */
;
/* let arr = createArr(len); */
//TODO: need regex based splitting function
let arr = ["In", "specific", "applications", "(such", "as", "digital", "distribution", "broadcast,", "similar", "toGoogle", "Play", "Store),", "smartphones", "can", "be", "used", "to", "carry", "out", "some", "attacks", "andtherefore", "increase", "the", "threats", "of", "such", "attacks"]
let pair = function(srcArr, usedArr) {
let count = 0,
genNum = null,
pair = [];
do {
let genNum = Math.floor(Math.random() * srcArr.length);
if (usedArr.indexOf(genNum) == -1) {
pair.push(genNum);
usedArr.push(genNum);
count++;
}
} while (count < 2)
return pair;
}
let makeUsedArr = function(srcArr, usedArr) {
do {
pair(srcArr, usedArr);
} while (srcArr.length > usedArr.length)
return usedArr;
}
let replace = function(srcArr, usedArr, replacedArr) {
//FIXME: feels weird, like double duty
let fullUsedArr = makeUsedArr(srcArr, usedArr);
fullUsedArr.forEach(function(each) {
replacedArr.push(srcArr[each]);
})
return replacedArr;
}
console.log('replace', replace(arr, used, replaced))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment