I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
I have it starting here because I thought arr[0]+1 would specify the number after the first element in the array, which would be appropriate as I’m trying to find the numbers between the two ascending (hopefully) elements. I thought it would work because the first element is specified by index 0.
function sumAll(arr) {
arr.sort((a,b)=>a-b)
let arr2=[]
for (let i=arr[0]+1;i<arr[1];i++)
arr2.push(i);
let sum=0
for (let j=0;j<arr.length;j++)
sum+=arr2[j]
return arr[0]+arr[1]+sum
}
console.log(sumAll([10, 5]));
I have updated the sort above.
It says it still does not work for [5,10] or [10,5]
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
Applying formatting so other people can read your code:
function sumAll(arr) {
arr.sort((a, b) => a - b);
let arr2 = [];
for (let i = arr[0] + 1; i < arr[1]; i++) {
arr2.push(i);
}
let sum = 0;
for (let j = 0; j < arr.length; j++) {
sum += arr2[j];
}
return arr[0] + arr[1] + sum;
}
console.log(sumAll([10, 5]));
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.