How to Count data in for loop nodejs - javascript

How to Count data in for loop nodejs
var service_count = 0;
for (let i = 0; i < res.length; i++) {
service_count += i;
console.log('service_charge_count: =====> '+service_count);
}

By adding i to your variable, you're adding in the first loop 0, in the second loop 1, in the third loop 2. So you're not counting anything.
var service_count=0;
for(let i=0;i<res.length;i++){
service_count++;
}
console.log('service_charge_count: =====> '+service_count);
By that you'll see how many iterations has had the loop. If the final value of service_count is the length of the variable res you can do this:
var service_count = res.length;
console.log('service_charge_count: =====> '+service_count);

Replace i with service_count, then you can change your code with:
for(let service_count = 0; service_count < res.length; service_count ++){
console.log('service_charge_count: =====> ' + service_count);
}
Or I think that you service_count is res.length, therefore, you can do:
let service_count = res.length;
console.log(service_count);

Related

Adding different number of '-' in every loop (javascript)

I need to create a string like this: 01-2--3---4----5-----6------7------- for a let n=7, using a loop.
I've created something like this so far:
let numbers = '';
n = 7;
for(i=0; i<=n; i++) {
numbers += i;
if (i>0) {
numbers += ('-')
}}
which gives me: "01-2-3-4-5-6-7-". Don't know how to change the code so that it could multiply the number of '-' equal n in every loop.
What you need is a nested loop, to iterate over the value of i in the outer loop.
const n = 7
let result=''
for (let i = 0; i <= n; i++) {
let dashes=''
for(let j=0; j < i; j++){
dashes+='-'
}
result+= `${i}${dashes}`
}
console.log(result)
You could use String#repeat for the wanted dash.
Do not forget to declare any variable.
let numbers = '',
n = 7;
for (let i = 0; i <= n; i++) numbers += i + '-'.repeat(i);
console.log(numbers);
This the solution for your problem:
var numbers = '';
var n = 7;
for(var i = 0; i <= n; i++) {
numbers += i;
for(var j = 0; j < i; j++) {
numbers += "-";
}
}

unable to loop the object in Angular

I have a data like below
data = ["I253,J665,l2575"]
and I need the the results like
I253,
J665,
l2575
when i tried to use for in i am getting like I253,J665,l2575 and I tried for loops also but not getting the result
let data = ["I253,J665,l2575"]
for (let i = 0; i > this.data.length; i++) {
console.log(i)
}
for (let x of this.data) {
console.log(x)
}
tried converting the data in to string and then using split changed into array but then also i am getting typeof object only
below is my stack blitz url =: https://stackblitz.com/edit/angular-ivy-drf1dk?file=src/app/app.component.ts
Modify your data variable like below:
data = ["I253", "J665", "l2575"];
for(let i = 0; i < this.data.length; i++){
console.log(this.data[i]);
}
If you have data variable as data = ["I253,J665,l2575"];
Then split it first and then loop through the generated array:
const arr = data[0].split(',');
for(let i = 0; i < arr.length; i++){
console.log(arr[i] + ',');
}
You were having multiple mistakes. First one was with for condition it should be i < this.data.length not i > this.data.length. Then you need to split and loop over it with for (let j = 0; j < data[i].split(',').length; j++) so data[i].split(',')[j] will return expected value.
In case of 2nd for...of loop you were simply logging whole value. Here also you need to split inside for...of and use one more loop to log.
Alternatively you can also use flatMap and loop over it like for (let m of data.flatMap(x => x.split(','))).
Try it below. You can use this.data, but it won't work in below example so it is used as simply data.
let data = ["I253,J665,l2575"];
console.log("Using for loop");
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].split(',').length; j++) {
console.log(data[i].split(',')[j]);
}
}
console.log("Using for...of loop");
for (let x of data) {
for (let y of x.split(',')) {
console.log(y);
}
}
console.log("Using flatMap");
for (let m of data.flatMap(x => x.split(','))) {
console.log(m);
}
Two ways to solve this.
Also note that your loop is wrong SHOULD NOT BE '>' and Should Be '<'
1. Your data is at array index zero so if you are to keep the data as is
let data = ["I253,J665,l2575"]
let splits = data[0].split(',')
for (let i = 0; i < splits.length; i++) {
console.log(splits[i])
}
or
let data = ["I253,J665,l2575"]
let splits = data[0].split(',')
for (let element of splits) {
console.log(element )
}
2. Fix the data string
let dataString = "I253,J665,l2575"
let splits = dataString.split(',')
for (let i = 0; i < splits.length; i++) {
console.log(splits[i])
}
or
let dataString = "I253,J665,l2575"
let splits = dataString.split(',')
for (let element of splits) {
console.log(i)
}
Clone of the example provided in question
https://stackblitz.com/edit/angular-ivy-izj7up

JavaScript Scope : please explain me why different result in and of loop creating variable

Can you please explain to my why i get to different results;
This is the finished code and the result I wished:
function largest_of_arrs(arr){
var largest_arr = [];
var holder;
var max = 1;
for(var i = 0; i < arr.length; i++){
var sum = 0;
for(var j = 0; j < arr[i].length; j++){
sum += arr[i][j];
if(sum > max) {
max = sum;
largest_arr = arr[i];
}
}
}
return largest_arr;
}
var array_1 = [[1,2,3,4,5],[1,2,2,3,1],[11,12,23,45,88],[20,20,5,5,1]];
largest_of_arrs(array_1);
This gives me the wished result: [11,12,23,45,88]
But when i declare the sum variable outside the for loop like this
function largest_of_arrs(arr){
var largest_arr = [];
var holder;
var max = 1;
var sum = 0;
for(var i = 0; i < arr.length; i++){
for(var j = 0; j < arr[i].length; j++){
sum += arr[i][j];
if(sum > max) {
max = sum;
largest_arr = arr[i];
}
}
}
return largest_arr;
}
var array_1 = [[1,2,3,4,5],[1,2,2,3,1],[11,12,23,45,88],[20,20,5,5,1]];
largest_of_arrs(array_1);
The result is: [20,20,5,5,1]
Can you please explain to me why ?, and how to scope here works?
if possible please with pictures (graphical) ..
Can you please explain to me why?
In the first case, you reset the value of sum to zero on each iteration of the outer loop. In the second case, you don't so sum just keeps accumulating.
and how to scope here works?
Variables declared with var have function scope (or global if declared globally). Your problem isn't to do with scope, it's because of where you assign 0 to sum.
if possible please with pictures (graphical)
Sorry, no pictures. :-(
First Case -
i=0
sum = 0;
j iterates till end of loop
sum = 1+2+3+4+5;
i=1
sum = 0;
j iterates till end of loop
sum = 1+2+2+3+1;
i=2
sum = 0;
j iterates till end of loop
sum = 11+12+23+45+88;
....
Second Case -
i=0
sum = 0;
j iterates till end of loop
sum = 1+2+3+4+5;
i=1
sum = 1+2+3+4+5;
j iterates till end of loop
sum = (1+2+3+4+5)+1+2+2+3+1;
i=2
sum = ((1+2+3+4+5)+1+2+2+3+1);
j iterates till end of loop
sum = ((1+2+3+4+5)+1+2+2+3+1)+11+12+23+45+88;
...
The for statement does not define a scope in javascript, only functions do (as per ES5).
Your problem is because the moment you assign 0 to sum: inside the outer for.

Can someone please tell me why this code wont run?

var howM = prompt("How many cards?")
var arr = [];
for(var i = 0; i < howM; i++)
arr.push(prompt("Enter a card:"));
console.log(arr)
for(var i = 0; i <= howM; i++)
var sum = 0;
var eXt = arr[i]
eXt = eXt.replace (/-/g, "");
for (i = 0; i < eXt.length; i++) {
sum += parseInt(eXt.substr(i, 1)); }
console.log(sum);
It tells me this "TypeError: Cannot read property 'replace' of undefined
at eval:13:11" which makes no sense to me because its right above it.
The intetended body of the loop for(var i = 0; i <= howM; i++) is not enclosed in braces {..}. As a result, only the statement var sum = 0; will be executed in the loop. Also, you probably meant to say i < howM. So you want something like this for the loop:
for(var i = 0; i < howM; i++) {
var sum = 0;
var eXt = arr[i]
eXt = eXt.replace (/-/g, "");
for (i = 0; i < eXt.length; i++) {
sum += parseInt(eXt.substr(i, 1));
}
}
console.log(sum);
Check the comments:
var howM = prompt("How many cards?")
var arr = [];
for(var i = 0; i < parseInt(howM); i++)
arr.push(prompt("Enter a card:")); //No curly braces is fine when its a single line. When there's no braces, JS just runs the next line x amount of times
console.log(arr)
var sum = 0; //Create sum out here. Setting it to zero every loop defeats the purpose
for(var i = 0; i < arr.length; i++)//You said "i <= howM". Better to use the length of the array that is being looped through
{ //Use curly braces to show what code to execute repeatedly
var eXt = arr[i]; //Set eXt to the current number
eXt = eXt.replace("-", ""); //No need for regex
sum += parseInt(eXt); //Convert the input to a number, then add it to sum
}
console.log(sum);
The second for loop doesn't have brackets around it. You can MUST use brackets UNLESS it is a one line loop. For example:
This is fine:
for (var i=0;i<100;i++)
console.log(i);
This is NOT:
for (var i=0;i<100;i++)
var x = i;
x++;
console.log(x);
So the second for loop should be this:
for(var i = 0; i <= howM; i++) {
var sum = 0;
var eXt = arr[i]
eXt = eXt.replace (/-/g, "");
for (i = 0; i < eXt.length; i++) {
sum += parseInt(eXt.substr(i, 1));
}
console.log(sum);
}
Also in the first for loop I would use arr[i] = value instead.

How do I get a sum of these numbers in array?

I want to use for. There are two things, one, the console.log doesn't work inside the for statement. Second, it is not summing up. code below:
var numbers = [1,2,3,4];
var total = 0;
for (var i= 0; numbers.length < i; i++){
total += numbers[i];
// console.log(total); doesn't work
}
// console.log(total); gives 0
Change condition in for should be i < numbers.length not numbers.length < i
var numbers = [1,2,3,4];
var total = 0;
for (var i= 0; i < numbers.length; i++){
total += numbers[i];
}
console.log(total);
Your for loop is exiting immediately because of the condition
for (var i = 0; numbers.length < i; i++) {
Because numbers.length (in this case) is 4, and i is 0, the for loop never executes.
You probably want it flipped around, to say something like
for (var i = 0; i < numbers.length; i++) {

Categories

Resources