Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
how would one write a function to find the missing element of an numeric array for example:
getMissingElement( [0, 5, 1, 3, 2, 9, 7, 6, 4] ) // returns 8
Is there just one element missing, and the others are for sure non-repeating?
Then recall that the formula to compute the sum of 0 + 1 + ... + (N-1) is (N-1)*N/2, and the difference from this to the sum in your array is the missing element:
function getMissingElement(array) {
var sum = 0;
var N = array.length + 1;
for(i = 0; i < N-1; ++i) {
sum += array[i];
}
return (N-1)*N/2 - sum;
}
function getMissingElement(myArray) {
myArray.sort();
myAray.reverse()
for(var i = 1; i < myArray.length; i++) {
if(myArray[i] - myArray[i-1] != 1) {
//log your numbers or print them or whatever you like
}
}
}
This is assuming the most basic definition of "missing item", where the missing item is between existing values on either side.
Here's way to do it:
arr.sort(function(x, y){return x - y})
.map(function(x, i, me){return me[i+1]-x > 1 && x+1})
.filter(Number)
This will give you an array with missing numbers, for [0,2,4] it will give you [1,3]
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 17 days ago.
Improve this question
I have to sum the numbers like sum of 55555 is 25 and sum of 25 is 7, but we have to use while loop specifically to solve it
function createCheckDigit(membershipId) {
string = membershipId.split('');
let sum = 0;
for (var i = 0; i \< string.length; i++) {
sum += parseInt(string\[i\],10);
}
return sum \>= 10 ? createCheckDigit(String(sum)) : sum;
}
console.log(createCheckDigit("55555"));
Now i have to do this using while loop. The final answer of the code will be 7 if the number is 55555.
Here is a simple implementation (strings can be iterated like arrays)
function createCheckDigit(membershipId) {
membershipId = String(membershipId)
let sum = i = 0
while (i < membershipId.length) {
sum += Number(membershipId[i++])
}
return (sum >= 10) ? createCheckDigit(sum) : sum
}
console.log(createCheckDigit("55555"))
console.log(createCheckDigit(77777))
To find the digital root of a number, you need to repeatedly sum its digits until it reachs a single-digit number
function createCheckDigit(membershipId) {
let sum = membershipId;
while (sum >= 10) {
let digits = sum.toString().split('');
sum = 0;
for (let i = 0; i < digits.length; i++) {
sum += parseInt(digits[i]);
}
}
return sum;
}
console.log(createCheckDigit("55555"))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
Why the output of the following code is not (1,2,3,4,5)?
var x = 0;
while (x < 6) {
x++;
}
document.write(x);
That is because you are only writing to the document once.
For this, you should probably use a for loop.
for (let x = 0+1; x < 5+1; x++) {
document.write(x);
}
This says x = 0 + 1 = 1 (so it starts at 0), then for every x > 5 + 1 (the +1 is so it ends at 5) do this, then add 1 to x.
Simply: x = 1, then when x is over 5, it will write to the document, then it will add 1 to x to continue the loop.
Each time the loop is repeated, the value in the x variable is updated.
x++ increases the value of x by one unit
And you write the final value
This example shows how to achieve this:
var x = 0;
let myArray = [];
while(x<6) {
++x; // We want x to start at 1
myArray.push(x) // Add the new value of x into the array
}
const string = '(' + myArray.join(',') + ')'; // Format the output string by joining the array in a csv format and wrapping with brackets.
document.write(string);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm supposed to get the multiplication value of the multi-dimensional array. but I am getting '1' as output whatever values being changed in array.
function arrayMultiplyer(arr){
var multi = 1;
for(var i=0;i < arr.length; i++){
for(var j =0; j<arr[i];j++){
multi *= arr[i][j];
}
}
return multi;
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
You need to check arr[i].length in the j loop.
function arrayMultiplyer(arr){
var multi = 1;
for(var i=0;i < arr.length; i++){
for(var j =0; j<arr[i].length;j++){ // you need to check arr[i].length here
multi *= arr[i][j];
}
}
return multi;
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
You can simply do this in two lines.
function arrayMultiplyer(arr){
let flattenedArray = arr.flat();
return flattenedArray.reduce((x, y) => x * y);
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
An alternative solution using .reduce()
function arrayMultiplier(arr) {
return arr.reduce((tot,arr2) =>
arr2.reduce((subTot, n) =>
subTot * n
, tot)
, 1);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
i have a very long array of points like so:
latlngs: [
[3.063895, 50.636767],
[3.06339, 50.637233],
[3.063309, 50.637278],
[3.063254, 50.637288],
[3.063103, 50.637267],
[3.061939, 50.636762],
[3.059679, 50.635821],
[3.056687, 50.634532],
[3.067972, 50.628265],
[3.068189, 50.628169],
[3.068389, 50.628159],
[3.06959, 50.628201],
[3.075613, 50.629068],
[3.077604, 50.629383],...
// 4500 array more
]
And i would like to loop on one on n element on it to make the computation less intensive. For example i would like to loop on 1 on 6 elements. What would be the best way to do that ?
A for loop seems like the simplest solution:
for (let n = startIndex; n < endIndex; ++n) {
const entry = theArray[n];
// ...
}
You can also create an array from a slice of an array, but it...creates an array (and, in this example, an iterator object, though that may get optimized away):
for (const entry of theArray.slice(startIndex, endIndex)) {
// ...
}
In a comment, user753642 said they think you mean "...elem 0, elem 6, elem 12". If so, you'd use n += 6 rather than ++n:
for (let n = startIndex; n < endIndex; n += 6) {
const entry = theArray[n];
// ...
}
That assumes you know endIndex is <= theArray.length. If you're accepting it from outside you may not know that, in which case:
for (let n = startIndex, end = Math.min(theArray.length, endIndex); n < end; n += 6) {
const entry = theArray[n];
// ...
}
A simple for loop is enough
for (let index = 0; index < latlngs.length; index+= 6) {
// do whatever computation you need with latlngs[index]
}
an alternative to the for loop with 6 increment can be the usage of modulo
latlngs.forEach((x, i) => {
// cbk + return in case of modulo !== 0 is negligible
// in regards to doyourstuff
if (i % 6 !== 0) { return }
doyourstuff
})
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have one string as "testaabbaccc" in this string we contain palindrome as "abba" and it's length is 4 but how can we identify this with a JavaScript code.
var string ="testaabbaccc"
Need Output as abba is palindrome and length is 4
You can use this article and modify it to your needs.
Working demo
function isPalindrome(s) {
var rev = s.split("").reverse().join("");
return s == rev;
}
function longestPalind(s) {
var maxp_length = 0,
maxp = '';
for (var i = 0; i < s.length; i++) {
var subs = s.substr(i, s.length);
for (var j = subs.length; j >= 0; j--) {
var sub_subs = subs.substr(0, j);
if (sub_subs.length <= 1)
continue;
if (isPalindrome(sub_subs)) {
if (sub_subs.length > maxp_length) {
maxp_length = sub_subs.length;
maxp = sub_subs;
}
}
}
}
return maxp;
}
console.log(longestPalind("testaabbaccc"));
console.log(longestPalind("testaabbaccc").length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>