I have an array from where I would like to find multiple modes. The problem is that my code prints the mode multiple times, but I would like to only print an array with 7 and 8 in it like this [7, 8] instead of [7, 7, 7, 7, 7, 8, 8, 8, 8, 8]. Can you please help me, how to fix it?
const array = [1, 1, 2, 2, 2, 5, 5, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9];
let maxStreak = 0;
let modus = [];
for (let i = 0; i < array.length; i++) {
var series = 0;
for (let j = 0; j < array.length; j++) {
if (array[i] == array[j]) {
++series;
}
}
if (series > maxStreak) {
maxStreak = series;
modus = [];
modus.push(array[i]);
} else if (series == maxStreak) {
modus.push(array[i]);
console.log(modus);
}
}
I tried to do it this way but I need it to print 7 and 8 in this case only one time.
Problem:
The problem in your code is that you just log it in every loop. But the final value of modus variable is what you want.
Solution:
If you move your console.log outside of the for loop(as I did in the sandbox) everything works fine.
const array = [1, 1, 2, 2, 2, 5, 5, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9];
let maxStreak = 0;
let modus = [];
for(let i = 0; i < array.length; i++){
let series = 0;
for(let j = i + 1; j < array.length; j++){
if(array[i] === array[j]){
++series;
}
}
if (series > maxStreak){
maxStreak = series;
modus=[];
modus.push(array[i]);
}else if(series == maxStreak){
modus.push(array[i]);
}
}
console.log(modus)
Related
When I run this I can't seem to get the rest of the values.
Write a function mergingTripletsAndQuints which takes in two arrays as arguments. This function will return a new array replacing the elements in array1 if they are divisible by 3 or 5. The number should be replaced with the sum of itself added to the element at the corresponding index in array2.
function mergingTripletsAndQuints(array1, array2) {
let result = [];
let ctr = 0;
let x = 0;
for (let i = 0; i < array1.length; i++) {
for (let j = 0; j < array2.length; j++) {
ctr = array1[i] + array2[j];
if (ctr % 3 === 0 || ctr % 5 === 0) {
result.push(ctr);
} else {
return array1[i];
}
}
}
return result;
}
console.log(mergingTripletsAndQuints([1, 2, 3, 4, 5, 15], [1, 3, 6, 7, 8, 9])); // expected log [1, 2, 9, 4, 13, 24]
console.log(mergingTripletsAndQuints([1, 1, 3, 9, 5, 15], [1, 2, 3, 4, 5, 6])); // expected log [1, 1, 6, 13, 10, 21]
It is only logging [1], [1]
I'm not sure, but I suppose there is a typo returning array1[i] in nested loop. I suppose you mean result.push(array1[i]) instead.
I think it should be something like this:
function mergingTripletsAndQuints(array1, array2) {
let result = [];
for (let i = 0; i < array1.length; i++) {
if (array1[i]% 3 === 0 || array1[i]% 5 === 0) {
result.push(array1[i] + array2[i]);
} else {
result.push(array1[i]);
}
}
return result;
}
console.log(mergingTripletsAndQuints([1, 2, 3, 4, 5, 15], [1, 3, 6, 7, 8, 9])); // expected log [1, 2, 9, 4, 13, 24]
console.log(mergingTripletsAndQuints([1, 1, 3, 9, 5, 15], [1, 2, 3, 4, 5, 6])); // expected log [1, 1, 6, 13, 10, 21]
A nested for loop is not necessary, look at this code:
function mergingTripletsAndQuints(array1, array2) {
let sum = [];
for (let i = 0; Math.max(i < array1.length, i < array2.length); i++) {
if (array1[i] % 3 == 0 || array1[i] % 5 == 0) {
sum.push(array1[i] + array2[i])
} else {
sum.push(array1[i])
}
}
return sum;
}
const grades = [9, 8, 5, 7, 7, 4, 9, 8, 8, 3, 6, 8, 5, 6];
for (let i = 0; i < grades.length; i++) {
if (grades[i] >= 8) {
console.log(grades[i])
}
}
I'm trying to log how many items from the array fulfil the condition. the output I'm looking for is : 6 (because 6 of the numbers are equal or greater than 8)
tried
let count = 0;
for (let i = 0; i < grades.length; i++) {
if (grades[i]>= 8){
count++
console.log(count)
}
}
function countGreaterThan8(grades){
// initialize the counter
let counter = 0;
for (let i = 0; i < grades.length; i++) {
// if the condition satisfied counter will be incremented 1
if (grades[i] >= 8) {
counter++;
}
}
return counter;
}
const grades = [9, 8, 5, 7, 7, 4, 9, 8, 8, 3, 6, 8, 5, 6];
console.log(countGreaterThan8(grades)); // 6
You can call Array.filter to create a new array containing only items that fulfill the condition. You can then make use of the length of the array however you want. Like this
const grades = [9, 8, 5, 7, 7, 4, 9, 8, 8, 3, 6, 8, 5, 6];
const gradesThatPassCondition = grades.filter(grade => grade > 6);
console.log(gradesThatPassCondition.length);
This question already has answers here:
Create copy of multi-dimensional array, not reference - JavaScript
(4 answers)
Closed 1 year ago.
What's wrong with this code?
let matrix1 = [
[2, 7, 9, 2],
[8, 0, 7, 1],
[8, 8, 0, 8]
];
let arr = []; // or arr = [[]];
for (let i = 0; i < matrix1.length; i++) {
for (let j = 0; j < matrix1[i].length; j++) {
arr[i][j] = matrix1[i][j];
}
}
console.log(arr);
Error is:
Cannot set property '0' of undefined
This is when I try to assign the value of an element of matrix1 to the new array.
for loop works for the single dimensional array.
try this
let matrix1 = [
[2, 7, 9, 2],
[8, 0, 7, 1],
[8, 8, 0, 8]
];
let arr = []; // or arr = [[]];
for (let i = 0; i < matrix1.length; i++) {
for (let j = 0; j < matrix1[i].length; j++) {
if(!arr[i])
arr[i] = []
arr[i][j] = matrix1[i][j];
}
}
if you want copy a 2d array without for loop try this one:
let matrix1 = [
[2, 7, 9, 2],
[8, 0, 7, 1],
[8, 8, 0, 8]
];
let arr = JSON.parse(JSON.stringify(matrix1))
You need to create a new sub array in the outer loop so that arr[i] is an array and not undefined
let matrix1 = [
[2, 7, 9, 2],
[8, 0, 7, 1],
[8, 8, 0, 8]
];
let arr = [];
for (let i = 0; i < matrix1.length; i++) {
// push a new sub array to be populated in next loop
arr.push([]);
for (let j = 0; j < matrix1[i].length; j++) {
arr[i][j] = matrix1[i][j];
}
}
console.log(arr);
The problem is the line arr[i][j] = matrix1[i][j];, since arr[i][j] is undefined at this point.
The correct way of adding elements to an array is using the .push() function:
let matrix1 = [
[2, 7, 9, 2],
[8, 0, 7, 1],
[8, 8, 0, 8]
];
let arr = [];
for (let i = 0; i < matrix1.length; i++) {
arr.push([]);
for (let j = 0; j < matrix1[i].length; j++) {
arr[i].push(matrix1[i][j]);
}
}
console.log(arr);
Also note that using JSON might achieve the same task in a simpler way:
let matrix1 = [
[2, 7, 9, 2],
[8, 0, 7, 1],
[8, 8, 0, 8]
];
let arr = JSON.parse(JSON.stringify(matrix1))
console.log(arr)
I'm trying to make it work on every number divisible by three.
here's my code:
var number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for(var i = 0; i<10 ; i++) {
if (i % 3 === 0) {
console.log(color[i]);
}
}
I think you need to use number[i] % 3 === 0. And what is the color there in your code? Change it into number.
var number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for(var i = 0; i<10 ; i++) {
if (number[i] % 3 === 0) {
console.log(number[i]);
}
}
Try this
var number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for(var i = 0; i<10 ; i++) {
if (i % 3 === 0) {
console.log(i);
}
}
I was testing out how splice works while iterating through an array, and don't understand why 0 stayed in the list?
var array = [2, 5, 9, 14, 0, 1, 3, 6, 7];
for (var i = 0; i < array.length; i++) {
if (array[i]%2 == 0) {
array.splice(i,1);
}
}
//0 % 2 == 0 is true, and yet
//array = [5, 9, 0, 1, 3, 7]
0 is getting skipped
You are mutating (changing) the array while you're iterating through it. This is a programming no-go.
Let's walk through...
i = 0 and 2 is even and gets spliced, your array is now [5, 9, 14, 0, 1, 3, 6, 7]
i = 1 and we didn't even check 5 which is in index 0 now... we're now checking 9 which is odd, fine
i = 2 and 14 is even and gets spliced, your array is now [5, 9, 0, 1, 3, 6, 7]
i = 3 and 0 gets skipped (as 0 is in index 2 now), 1 is odd, fine
i = 4 is odd fine
i = 5 is even and get spliced
i = 6 is odd fine
What you really want is this...
Array.prototype.filter = function(func) {
var result = new Array();
for (var i = 0; i < this.length; ++i)
if (func(this[i]))
result.push(this[i]);
return result;
}
values = [2, 5, 9, 14, 0, 1, 3, 6, 7];
odd_only = values.filter(function(x) { x % 2 != 0; });
Every time you remove a value from the array, you skip the one that follows it, because the array is reindexed on every splice. You can loop backwards instead:
var array = [2, 5, 9, 14, 0, 1, 3, 6, 7];
for (var i = array.length-1; i >= 0; i--) {
if (array[i]%2 == 0) {
array.splice(i,1);
}
}
It skips the 0 because splice re-indexes the array.
use this:
var array = [2, 5, 9, 14, 0, 1, 3, 6, 7];
for (var i = 0; i < array.length; i++) {
if (array[i]%2 == 0) {
array.splice(i,1);
i = i - 1;
}
}