How to iterate by lines vertical in js? Like in the Triangle task

Back to General discussions forum

Stas Yasinskiy     2022-10-20 10:34:45
let tringle = input().split();   
for(i=0; i<tringle.length; i++) {
    output(tringle[i]);
}

input = 578 293 628
698 1079 1182
987 1606 3744
981 1559 632
...
JesseDai     2022-10-20 14:19:48
User avatar

Hello,

  1. What exactly do you mean by "iterate by lines vertical"?
  2. Where did you get this input data? The input data provided for the Triangle task is in a different format.
Stas Yasinskiy     2022-10-21 08:55:34

Task #9 When I iterate over input data by for loop this method gives only the first triple digits.

JesseDai     2022-10-21 14:55:25
User avatar

I still don't understand what you mean. What output are you getting? If you submit your answer + code (not copying to forum), I will be able to see the code, even if the answer is wrong.

If you're trying to process the data given by Problem #9, read on.

The first line of data should tell you how many iterations to loop for. What I would do is read in the first line as n, then loop (i=0; i<n; i++){ ... }. You will need to call input() inside of your loop body to read each subsequent line.

Stas Yasinskiy     2022-10-22 07:54:17
let triangle = input().split(' ');
let S = 0;
for(i=0; i<triangle.length; i++) {
    S += + tringle[i] / 2;
}
let sum = S * ( S - triangle[0]) * ( S - triangle[1]) * ( S - triangle[2]);
    if(sum <= 0) {
        sum = 0;
    } else {
    sum = 1;
}
output(sum);
Rodion (admin)     2022-10-22 17:12:58
User avatar

Stas, Hi!

Every input() allows you to assign next line of input into the variable.

So your input() should be inside the loop (but you have it outside).

E.g.

let numLines = input() * 1;  // I use *1 to make number from string, I'm poor at javascript

for (let i = 0; i < numLines; i++) {
    let nextTriangle = input().split(' ');
    // ... do something
}
Stas Yasinskiy     2022-10-23 16:10:11

Thank you guys for your help something becomes clearer

Please login and solve 5 problems to be able to post at forum