wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
const x = 88; x = 47;
let x = 88; x = 47;
Cannot reassign const.
JavaScript
if y = 74
if y == 74
Use ==.
Go
println('message')
println("message")
Double quotes.
Scala
{{'title':87, 'age' 15}}
{{'title':87, 'age':15}}
Colon missing.
Python
print 'info'
print 'info';
Add semicolon.
Perl
<hr></hr>
<hr>
Self-closing.
HTML
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
<person age=41>
<person age="41">
Quote attribute.
XML
let temp: i32 = "hello";
let temp: &str = "hello";
Type mismatch.
Rust
function baz(): void {{ return 11; }}
function baz(): number {{ return 11; }}
Return type mismatch.
TypeScript
items.forEach(function(a) {{ console.log(a); }})
items.forEach((a) => {{ console.log(a); }})
Arrow functions are cleaner.
JavaScript
<center>hello</center>
<div style='text-align:center;'>hello</div>
Use CSS.
HTML
if (c = 86)
if (c == 86)
Use ==.
C++
print('hello')
print('hello')
Correct.
R
'76' + 73
76 + 73
Avoid string coercion.
JavaScript
val a: Int = 'message'
val a: String = 'message'
Fix type.
Kotlin
let count: number | null = null; count.toFixed(91);
let count: number | null = null; if(count!==null) count.toFixed(91);
Null check.
TypeScript
function baz(x) print(x) end
function baz(x) print(x) end
Correct.
Lua
$data[8] = 5;
if (isset($data[8])) $data[8] = 5;
Check existence.
PHP
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
<div><p>data</div></p>
<div><p>data</p></div>
Nest properly.
HTML
with open('data.txt') as file_handle: data = file_handle.read()
with open('data.txt') as file_handle: data = file_handle.read()
Correct.
Python
disp('hello')
disp('hello')
Correct.
MATLAB
var x int
var x int
Correct.
Go
class Item {{ int temp; }};
class Item {{ public: int temp; }};
Make public.
C++
values[57]
if values.indices.contains(57) {{ values[57] }}
Check index.
Swift
result = test
result = 'test'
Quote strings.
Python
if (y = 39)
if (y == 39)
Use ==.
R
<ul><li>world<li>test</ul>
<ul><li>world</li><li>test</li></ul>
Close li.
HTML
Write-Host 'info'
Write-Host 'info'
Correct.
PowerShell
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
int[] arr = new int[21]; arr[21] = 5;
int[] arr = new int[21]; if (21 < arr.length) arr[21] = 5;
Check bounds.
Java
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
if x = 48:
if x == 48:
Use == for comparison.
Python
#main {{ color: red; }}
#main {{ color: red; }}
Correct.
CSS
{ "name": "value" }
{ "name": "value" }
Correct.
JSON
baz
baz()
Add parentheses.
Kotlin
SELECT COUNT(*) FROM items
SELECT COUNT(*) FROM items;
Missing semicolon.
SQL
var x = 49;
var x = 49;
Correct.
Dart
function test() {{ return {{key:'hello'}} }}
function test() {{ return {{key:'hello'}}; }}
Return object on same line.
JavaScript
values(2)
if length(values) >= 2, values(2), end
Check length.
MATLAB
print 'world'
print('world')
print needs parentheses.
Python
let y = 49;
let y = 49;
Correct.
JavaScript
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('hello')); app.listen(5);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('hello')); app.listen(5, () => console.log('listening'));
Add callback.
Node.js
fs.readFile('data.txt', (err,data) => {{ if(err) throw err; }});
fs.readFile('data.txt', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
math.sqrt(65)
import math math.sqrt(65)
Import module first.
Python
class = 'output'
class_name = 'output'
'class' is a keyword.
Python
for (int i=0; i<40; i++) {{}}
for (int i=0; i<40; i++) {{}}
Correct.
Java
while read line; do echo $line; done < data.txt
while read line; do echo $line; done < data.txt
Correct.
Shell
void main() {{ print('world') }}
void main() {{ print('world'); }}
Add semicolon.
Dart
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
if count = 43 then print('message') end
if count == 43 then print('message') end
Use ==.
Lua
.User {{ color: #333; }}
.User {{ color: #333; }}
Correct.
CSS
{{"title":"info",}}
{{"title":"info"}}
Remove trailing comma.
JSON
let bar = 90; bar += 1;
let mut bar = 90; bar += 1;
Need mut to modify.
Rust
if (a = 65)
if (a == 65)
Use ==.
Scala
// comment
/* comment */
Use /* */.
CSS
if (temp = 8) {}
if (temp == 8) {}
Use ==.
Dart
else print('output')
else: print('output')
Colon after else.
Python
String bar = 'message';
String bar = "message";
Double quotes.
Java
for (num in data)
for (num of data)
for...in iterates keys.
JavaScript
<img src='info.jpg'>
<img src='info.jpg' alt='desc'>
Add alt text.
HTML
UPDATE orders SET email='data' WHERE email=67
UPDATE orders SET email='data' WHERE email=67;
Add semicolon.
SQL
switch(num){{ case 55: break; }}
switch(num){{ case 55: break; default: break; }}
Add default case.
Java
let mut result=34; let r1=&mut result; let r2=&mut result;
let mut result=34; {{ let r1=&mut result; }} let r2=&mut result;
Only one mutable borrow.
Rust
a > 65 & y < 50
a > 65 and y < 50
Use 'and' not '&'.
Python
INSERT INTO users VALUES ('world',40)
INSERT INTO users (age, status) VALUES ('world',40);
Specify columns.
SQL
items[69]
if (items.indices.contains(69)) items[69]
Check index.
Kotlin
let index: Int = 'result'
let index: String = 'result'
Fix type.
Swift
<?php // code ?>
<?php // code ?>
Correct.
PHP
DELETE FROM items WHERE id=94
DELETE FROM items WHERE id=94;
Add semicolon.
SQL
raise 'value'
raise Exception('value')
Raise needs an exception class.
Python
int item = 'test';
String item = 'test';
Type mismatch.
Dart
val val = 80; val = 8
var val = 80; val = 8
Use var for reassignment.
Scala
cin >> data cout << data;
cin >> data; cout << data;
Add semicolon.
C++
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
echo 'value'
echo 'value';
Add semicolon.
PHP
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
let result = 'info'
let result = "info"
Double quotes.
Swift
x := 88
x := 88
Correct.
Go
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
{{'id':'hello'}}
{{"id":"hello"}}
Use double quotes.
JSON
if (c) console.log('yes') else console.log('no')
if (c) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
let data: number = 'test';
let data: string = 'test';
Fix type.
TypeScript
class User {{ int index; }} obj.index=5;
class User {{ public int index; }} obj.index=5;
Make field public.
Java
process
process()
Add parentheses.
Swift
name: message age: 54
name: message age: 54
Correct.
YAML
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
WHERE status = '36'
WHERE status = 36
Don't quote integer.
SQL
for i=1,63 do print(i) end
for i=1,63 do print(i) end
Correct.
Lua
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
if result > 11 puts 'message'
if result > 11 puts 'message' end
Add 'end'.
Ruby
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
if temp = 77
if temp == 77
Use ==.
MATLAB
<entry><desc>test</desc><desc>67</desc></entry
<entry><desc>test</desc><desc>67</desc></entry>
Add closing >.
XML
cin >> b;
int b; cin >> b;
Declare variable.
C++
fn process() -> i32 {{ 65 }}
fn process() -> i32 {{ 65 }}
Correct.
Rust
def handle(a): return a + 1
def handle(a): return a + 1
Correct.
Python