How to log how many possibilities fulfil the if statement javascript - javascript

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);

Related

Printing multiple modes from array

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)

Loops & Control Flow

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;
}

Comparing Elements Values In An Array, and returning Boolean

Can anyone help I am nearly there I can get the code to return true for the first 2 test below, but not return false for the next 2 tests, what I need to do is to check that all the numbers in the array are in ascending order so every element number is greater than the last for the length of the array. Any ideas? Kind regards Jon.
Test.expect(inAscOrder([1, 2, 4, 7, 19])
Test.expect(inAscOrder([1, 2, 3, 4, 5])
Test.expect(!inAscOrder([1, 6, 10, 18, 2, 4, 20])
Test.expect(!inAscOrder([9, 8, 7, 6, 5, 4, 3, 2, 1])
function inAscOrder(arr) {
let result = false;
for (let i = 0; i <= arr.length; i++) {
for (let k = 0; k <= arr.length; k++) {
if (arr[i] < arr[k+1]) {
result = true;
}
}
}
return result;
}
You were very close to answer
function inAscOrder(arr) {
let result = true;
for (let i = 0; i <= arr.length; i++) {
if (arr[i] > arr[i+1]) {
result = false;
break;
}
}
return result;
}
Try this,
You can use the functional way instead.
const inAscOrder = arr => arr.slice(1).every((elem,i) => elem > arr[i]);
console.log(inAscOrder([1,2,5]));
The easiest way to compare two arrays, is to convert them to a strings and then compare the strings.
const isAscending = (arr) => arr.join("") == arr.sort((a, b) => a - b).join("");
console.log(isAscending([1, 2, 4, 7, 19]));
console.log(isAscending([1, 2, 3, 4, 5]));
console.log(isAscending([1, 6, 10, 18, 2, 4, 20]));
console.log(isAscending([9, 8, 7, 6, 5, 4, 3, 2, 1]));
For every element in the array, return true if it's the first element or it's greater than the previous element.
const isAscending = (arr) => arr.every((el, i, arr) => i === 0 || el > arr[i - 1]);
console.log(isAscending([1, 2, 4, 7, 19]));
console.log(isAscending([1, 2, 3, 4, 5]));
console.log(isAscending([1, 6, 10, 18, 2, 4, 20]));
console.log(!isAscending([9, 8, 7, 6, 5, 4, 3, 2, 1]));

What is the difference between these two chunks of code?

Can someone please help me understand the differences between these two programs? I was doing this RallyCoding Prep problem and wrote the first program (and it failed). Then I checked what others did to try and understand but it seems to me that the two programs do basically the same thing, written differently. Can someone help me understand why mine fails and the second program works?
Link to problem: https://www.rallycoding.com/problems/205
--My code--
let k = 16;
let length = Math.sqrt(k);
let height = length;
let arr = []
for(let i = 0; i < length; i++){
let row = [];
for(let x = 1; x <= k; x++){
row.push(x);
}
arr.push(row);
}
console.log(arr)
--Their code--
let k = 16
let c = 1;
const r = Math.sqrt(k);
const result = [];
while(c <= k) {
const block = [];
for (let i = 1; i <= r; i++) {
block.push(c);
c++;
}
result.push(block);
}
console.log(result)
I'm certain I'm missing something very obvious. I hope someone can help me understand this. Thank you!
Since the value of k = 16, the inner loop runs 16 times. So, the result produced by your code is
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
]
And the result produced by their code is
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
]
So, you can make these changes in your code to get the desired result.
let k = 16;
let length = Math.sqrt(k);
let height = length;
let arr = []
for(let i = 0; i < length; i++){
let row = [];
for(let x = 1; x <= length; x++){
row.push(x + i * length);
}
arr.push(row);
}
You code produces data like this: [[1, 2, 3], [1, 2, 3], ...]
(Each sub-array starts with 1)
You need to produce: [[1, 2, 3], [4, 5, 6], ...]
(Each sub array continues the sequence of the previous one)
the nested forloop you created loop over 16 times
for(let x = 1; x <= k; x++){ row.push(x); }
because K=16 so it will provide an array with 16 element.
the forloop keep runing intel it finish the job.but in the correction they use a "while" loop because The 'while' loop used only when the number of iteration are not exactly known.the are some diff between the two.
Here's a link you can find more

Adding an if condition to a for loop

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);
}
}

Categories

Resources