Tell us what’s happening:
I Have written the code for this challenge as follows
Your code so far
function pairwise(arr, arg) {
let sum = [];
let sumArray = [];
// Step 1
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length ; j++) {
sum.push([arr[i],arr[j]])
}
}
// Filter any item which has string in it.
console.log(sum)
// Step 2
for (let k = 0; k < sum.length; k++) {
for (let l = 0; l < sum[k].length; l++) {
if (sum [k][l + 1] != undefined) {
if (sum[k][l] + sum[k][l + 1] == arg) {
// Get the Indices of the element so matched
// Sum the indices
sumArray.push(arr.indexOf( sum[k][l] ) + arr.indexOf( sum[k][l + 1] ))
}
}
}
console.log("
")
}
let newSum = 0;
sumArray.forEach((element, key) => {
newSum += element;
})
console.log(newSum)
return newSum;
}
pairwise([1, 4, 2, 3, 0, 5], 7)
However, the code only passes 3 test cases and the 2 test cases remain unfulfilled which are as follows:
pairwise([1, 1, 1], 2) should return 1
&
pairwise([0, 0, 0, 0, 1, 1], 1) should return 10
pairwise([1, 1, 1], 2
returns 0 &
pairwise([0, 0, 0, 0, 1, 1], 1)
returns 32.
To cope up with this problem i tried filtering my array using following approach:
sum = sum.filter((element) => (!element.includes('nope')))
sum = sum.filter((element) => !element.includes('nopetoo'))
at the end of the first for loop like this:
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length ; j++) {
sum.push([arr[i],arr[j]])
}
}
// Filter any item which has string in it.
sum = sum.filter((element) => (!element.includes('nope')))
sum = sum.filter((element) => !element.includes('nopetoo'))
.
.
.
but when i run the updated code it fails all the test except the last one (which passes in any case).
Please Have a look and suggest something on what’s going on here…
Many thanks in advance
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.
Challenge: Pairwise
Link to the challenge: