Convert python code to javascript

Hello developers,
I got stack with codeforce 152A problem , I found a solution in python but I don’t know python , anybody can convert it from python to javascript ?

problem link

data = input().split()

N, M = int(data[0]), int(data[1])

marks = [None] * N
for i in range(N):
    marks[i] = input()

answer = 0
subj = []
if N == 1:
    print(N)
else:
    for i in range(N):
        tmp_answer = 0
        for col in range(M):
            flag = 1
            for j in range(N):
                if int(marks[j][col]) > int(marks[i][col]):
                    flag = 0
            if flag == 1:
                tmp_answer = 1
        if tmp_answer == 1:
            answer += 1

    print(answer)

Writing code for you isn’t really something we do here. What parts of the code don’t you understand so we can help you do the conversion?

1 Like

my implementation , but doesn’t works

function group(numberAndGroup, data){
  let n = numberAndGroup[0]
  let m = numberAndGroup[1]
  let answer = 0
  let subj= []
  const marks=[]

  
  if(n === 1){
    return n
  } else {
    for(let i=0; i<n; i++){
        let flag =1
        let tmp_answer=0;
     
      for(let c=0; c<m; c++){
        
      
      for(let j=0; j<n; j++){
        if(marks[j][c] > marks[i][c]){
          flag = 0
        }
        if(flag === 1) {
          tmp_answer = 1
        }
        if (tmp_answer === 1){
          answer +=1
        }
        
      }
      }
    }
  }
  return answer
  
}

console.log(group([3,3],[223,232,112]))

It doesn’t work could mean many things. How doesn’t it work?

It looks like your braces don’t agree with the Python indentation. If put the control statements on the same indentation and then put braces for every control statement, that should help.

1 Like

I am getting this error, so i am confused

Error: Uncaught TypeError: Cannot read properties of undefined (reading '0') on line 39

Yeah, I’d guess your brace mismatch is causing you to access variables incorrectly. Python forces users to use whitespace meaningfully. Your translation has to respect the logic expressed by the whitespace.

I am sorry, If you give me a solution it would be great, I am really confused where to solve.

As I said, we aren’t a magic free code machine. We are a ‘help you fix your code’ machine.

Look at the difference between your indentation and the Python indentation. They don’t match. Use that as a guide.

Python:

result = 0
if a:
  result += 1
  if b:
    result += 2

Javascript:

let result = 0;
if (a) {
  result += 1;
  if (b) {
    result += 2;
 }
}

You are un-nesting some of the indentation levels, which is leading to different results.

Bad Javascript translation:

let result = 0;
if (a) {
  result += 1;
}
if (b) {
    result += 2;
}

You are mostly correct, but you nesting/indentation does not agree.

thank you for your time Jeremy

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.