Write a program that allows for an integer array to be passed in and will then output all of the pairs that sum up
to 10. Please provide a solution that allows for
1) output all pairs (includes duplicates and the reversed ordered pairs),
2) output unique pairs only once (removes the duplicates but includes the reversed ordered pairs), and
3) output the same combo pair only once (removes the reversed ordered pairs). For example passing in [1, 1, 2, 4, 4,
5, 5, 5, 6, 7, 9] the following
let arr = [1, 1, 2, 4, 4, 5, 5, 5, 6, 7, 9]
let emptyArr = []
let uniquePairs = new Set()
let comboPairs = new Set()
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
if (arr[i] + arr[j] == 10) {
emptyArr.push([arr[i], arr[j]])
let pair = [arr[i], arr[j]]
uniquePairs.add(pair.toString())
let pairing = [arr[i], arr[j]].sort()
comboPairs.add(pairing.toString())
}
}
}
What will be the output of following code and why ? How Would You Modify the code to print 0 , 1 , 2 , 3 , 4 at
interval of 1 Seconds
for (var i = 0; i < 5; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}
To Change The var to let or const because the var is global scope and let and const are block scope
for (let i = 0; i < 5; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}
Flatten A deeply nested array
let NumArr = [1, [2, [3, 4], 5], 6]
let empNum = []
for(let i = 0; i < NumArr.length; i++){
if (NumArr[i].length) {
for(let j = 0; j < NumArr[i].length; j++){
if (NumArr[j].length) {
for(let k = 0; k < NumArr[i][j].length; k++){
empNum.push(NumArr[i][j][k])
}
}else{
empNum.push(NumArr[i][j])
}
}
}
else{
empNum.push(NumArr[i])
}
}
console.log(empNum)
Rotate Array [1, 2, 3, 4, 5, 6, 7] and k = 3 Give An Ouput [5,6,7,1,2,3,4]
let inpArr = [1, 2, 3, 4, 5, 6, 7]
let k = 3;
let a =inpArr.splice(-k);
let b =inpArr.splice(0,k +1);
console.log([...a , ...b]);