Find mode using Javascript with 2 rules based on my code [closed] - javascript

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 6 years ago.
Improve this question
so this is what I've achieved so far
function arrayMode(sequence) {
var arr= [];
var mostFreq = 1;
for(var i = 1; i <= 10; i++)
arr[i] = 0;
for(var i = 0 ; i < sequence.length; i++)
arr[sequence[i]]++;
for(var i = 1; i < 10; i++){
if( arr[i] > arr[mostFreq])
mostFreq = i;
}
return mostFreq;
}
so the scope for my case are
The sequence value is >= 1 and <=10
The sequence length is > 0
The sequence is an array of integer
Example 1
Input : sequence = [1, 10, 10]
Output : 10
Example 2
Input : sequence = [1, 3, 3, 3, 1]
Output : 3
It seems easy, I've tried to figure out but I cant find where is the mistake in my code, It's seems legit for me

As suggested by Maxim Krizhanovsky, the last loop should go from 1 to 10 instead of to sequence.length. Here is the working code
function arrayMode(sequence) {
var arr= [];
var mostFreq = 1;
for(var i = 0; i <= 10; i++)
arr.push(0);
for(var i = 0 ; i < sequence.length; i++)
arr[sequence[i]]++;
for(var i = 1; i <= 10; i++){
if( arr[i] > arr[mostFreq])
mostFreq = i;
}
return mostFreq;
}
The output is 10 and 3 respectively to your input.

Related

Array in each position successively Javascript [closed]

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 1 year ago.
Improve this question
I've got trouble with arrays. I'm trying to iterate elements for each position in the array using javascript.
so, I need to do that on the first position, be a value [1]. On the second position, [2, 3, 4]. on the third position, [5,6,7,8,9] and successively two by two.
My attempt was did a for loop:
for (let i = 1; i <= 5; i++) {
for (let j = 1; j <= 5; j++) {
console.log(`${i} ${j}`)
}
}
But while looping, the indices (i) repeat with (j).
What to do?
Try this code.
var counting = 1;
for (let i = 1; i <= 5; i = i + 2) {
var str = "";
for (let j = 1; j <= i; j++) {
str += `${counting} `;
counting++;
}
console.log(str);
}

get all 3 digit number whose sum is 11 or we can say module is 1 in JS [closed]

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 2 years ago.
Improve this question
In response I want all 3 digit numbers whose sum is 11 and also gives module 1 and
also want to know how can I push 000 in array in js.
For example number = 128, sum = 11 and module(remainder) = 1.
Like this I want all that numbers in array.
Can anyone share the logic for the same.
Thanx in advance.
I'm not sure what the second check is supposed to be (the modulus of something has to be 1). If that is supposed to be that the modulus of all digits of the number has to be 1, here is how you could do it:
var results = [];
for (let i = 1; i < 10; i++) {
for (let j = 0; j < 10; j++) {
for (let k = 0; k < 10; k++) {
if (i + j + k === 11 && i % j % k === 1) {
results.push(i.toString() + j.toString() + k.toString());
}
}
}
}
console.log(results);
OR:
var results = [];
for (let i = 100; i < 1000; i++) {
const [one, two, three] = i.toString().split('').map(i => +i);
if (one + two + three === 11 && one % two % three === 1) {
results.push(i.toString());
}
}
console.log(results);
Basically just go through all the possible combination and do the checks on each number.
Is this what you want?
var arr=[1,128,15];
var remainder = 127;
var result = arr.filter(function(i) {
var str=i.toString().split('');
var sum=0;
str.forEach(function(c) { return sum+=Number(c); });
return sum===11 && (i%remainder)===1;
});
and more fancy and ES6:
const arr=[1,128,15];
const remainder = 127;
const result = arr.filter(i=>{
const sum=i.toString().split('').reduce((s,v)=>(+v)+s,0);
return sum===11 && (i%remainder)===1;
});
You could take a brute force approach and check the sum with eleven. A check with the remainder is not necessary, because 11 has always a rest of one, if divided by ten.
const
getSum = v => [...v.toString()].reduce((a, b) => a + +b, 0),
values = [];
for (let i = 1; i < 1000; i++) {
if (getSum(i) === 11) values.push(i);
}
console.log(values);
.as-console-wrapper { max-height: 100% !important; top: 0; }

How to loop on a number [closed]

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 4 years ago.
Improve this question
How to loop on a number using jQuery from 1 to 10.
Let's say I have
var start=8;
var end=13;
Expected Output:
8 9 10 1 2 3
On reaching 10, the output series restarts from 1.
Try this
var start = 8, end = 13;
for(var i = start; i <= end; i++){
var num = i > 10 ? i % 10 : i;
console.log(num);
}
Try this
var start = 8;
var end = 13;
for(var i = start; i <= end; i++){
var result = i % 10;
if(result == 0){
result = 10;
}
console.log(result);
}
Try this it is quite straightforward.
var start = 8, end = 13;
for(var i = start; i <= end; i++){
console.log(i % 10 === 0 ? 10: i % 10);
}
var start = 5,
end = 13;
var i = start;
var j = start;
while (j >= start && j < end) {
console.log(i);
i = (i + 1) % 10;
j++;
}

Javascript Multiplication Table Array [closed]

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 8 years ago.
Improve this question
So I have been trying to do this for a while and I can't quite get it. I basically want a 2d array that is a multiplication table. So if I reference multTable[5][5] I would get 25. I have found scripts for a table that is printed but not one for an array. This is the best code I have so far.
var multTable;
for(var v = 0; i<13; v++) {
for(var i = 0; i<13; i++) {
multTable[v][i]=i*v
}
}
Javascript doesn't have built-in type of multi-dimensional array. So you can't declare multTable first and then directly use multTable[v][i]=i*v. You need to create an array of arrays.
var multTable = [];
for (var v = 0; v < 13; v++) {
multTable.push([]);
for (var i = 0; i < 13; i++) {
multTable[v].push(i * v);
}
}
multTable[5][5] // 25
Or alternatively, you can use object.
var multTable = {};
for (var v = 0; v < 13; v++) {
multTable[v] = {};
for (var i = 0; i < 13; i++) {
multTable[v][i] = i * v;
}
}
multTable[5][5] // 25
I'm not exactly sure what the question is, but it looks like your first for loop exit condition is using the wrong variable. Try the following:
var multTable;
for(var v = 0; v<13; v++) {
for(var i = 0; i<13; i++) {
multTable[v][i]=i*v
}
}

Javascript : Console loop [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I've just been challenged with the following question by a friend:
var i, n = 20;
for(i = 0; i < n; i--) console.log("-");
Q) Change a single symbol (change,delete,add) to make the code print "-" 20 times.
I've reached the following answers:
var i, n = 20;
for(i = 0; -i < n; i--) console.log("-");
var i, n = 20;
for(i = 0; i < n; n--) console.log("-");
He tells me there are three answers, but I can only come up with two.
Any ideas? This is driving me mad.
for(i = 0; i + n; i--) console.log("-");
Make n MINUS 20
var i, n = -20;
for(i = 0; i < n; i--) console.log("-");

Categories