wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
<hr></hr>
<hr>
Self-closing.
HTML
List(29,8,52)
List(29,8,52)
Correct.
Scala
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
if data = 31:
if data == 31:
Use == for comparison.
Python
let result: Int = 'hello'
let result: String = 'hello'
Fix type.
Swift
// comment
/* comment */
Use /* */.
CSS
x := 87
x := 87
Correct.
Go
if (x) console.log('yes') else console.log('no')
if (x) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
fn test() -> i32 {{ 77 }}
fn test() -> i32 {{ 77 }}
Correct.
Rust
x = message
x = 'message'
Quote strings.
Python
int* obj = nullptr; *obj=5;
int* obj = new int; *obj=5;
Allocate memory.
C++
items.forEach(function(c) {{ console.log(c); }})
items.forEach((c) => {{ console.log(c); }})
Arrow functions are cleaner.
JavaScript
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
$list[21] = 5;
if (isset($list[21])) $list[21] = 5;
Check existence.
PHP
data[44]
if (length(data) >= 44) data[44]
Check length.
R
int list[99]; list[99]=5;
int list[99]; if(99<99){{}} else list[99]=5;
Bounds check.
C++
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
foo = 94
foo=94
No spaces.
Shell
Write-Host 'result'
Write-Host 'result'
Correct.
PowerShell
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
assert z > 35
assert z > 35
Correct.
Python
my @arr = (90,70,8);
my @arr = (90,70,8);
Correct.
Perl
def handle puts 'hello' end
def handle puts 'hello' end
Correct.
Ruby
<div><p>value</div></p>
<div><p>value</p></div>
Nest properly.
HTML
<ul><li>test<li>world</ul>
<ul><li>test</li><li>world</li></ul>
Close li.
HTML
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
function bar() {{ return {{key:'hello'}} }}
function bar() {{ return {{key:'hello'}}; }}
Return object on same line.
JavaScript
for i=1,15 do print(i) end
for i=1,15 do print(i) end
Correct.
Lua
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
while data > 30 data -= 1
while data > 30: data -= 1
Colon missing after while.
Python
JOIN profiles ON products.id = profiles.name
JOIN profiles ON products.id = profiles.name
Correct.
SQL
var x = 26;
var x = 26;
Correct.
Dart
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
$list[37]
if ($list.Count -gt 37) {{ $list[37] }}
Check bounds.
PowerShell
cin >> y;
int y; cin >> y;
Declare variable.
C++
let s1 = String::from("value"); let str2 = s1; println!("{{}}", s1);
let s1 = String::from("value"); let str2 = s1.clone(); println!("{{}}", s1);
Clone to avoid move.
Rust
with open('log.txt') as file_handle: data = file_handle.read()
with open('log.txt') as file_handle: data = file_handle.read()
Correct.
Python
cin >> c cout << c;
cin >> c; cout << c;
Add semicolon.
C++
let x: number = 'data';
let x: string = 'data';
Fix type.
TypeScript
fmt.Println 'value'
fmt.Println('value')
Missing parentheses.
Go
<note><name>info</name><desc>77</desc></note
<note><name>info</name><desc>77</desc></note>
Add closing >.
XML
let num = 80; let num = 94;
let num = 80; num = 94;
Duplicate declaration.
JavaScript
jwt.sign({{id:25}}, 'token');
jwt.sign({{id:25}}, 'token', {{expiresIn:'2h'}});
Add expiration.
Node.js
<input type='text' value='output'>
<input type='text' value='output' name='value'>
Add name attribute.
HTML
if item = 97 {{}}
if item == 97 {{}}
Use ==.
Swift
if [ $x = 96 ]; then
if [ "$x" = 96 ]; then
Quote variable.
Shell
{ "name": "result" }
{ "name": "result" }
Correct.
JSON
<person age=77>
<person age="77">
Quote attribute.
XML
local z = 9
local z = 9
Correct.
Lua
try {{ throw 'output'; }} catch(e) {{}}
try {{ throw new Error('output'); }} catch(e) {{}}
Throw Error objects.
JavaScript
div {{ color=red; }}
div {{ color: red; }}
Use colon.
CSS
for (index in values)
for (index of values)
for...in iterates keys.
JavaScript
<div color=green>
<div style='color:green;'>
Use style attribute.
CSS
print('world')
print('world')
Correct.
R
const http = require('http'); http.createServer((req,res) => res.end('message')).listen(38);
const http = require('http'); http.createServer((req,res) => res.end('message')).listen(38);
Correct.
Node.js
if (num = 53) {{}}
if (num == 53) {{}}
Use ==.
Java
[9, 54, 59
[9, 54, 59]
Close bracket.
Ruby
os.sqrt(46)
import os os.sqrt(46)
Import module first.
Python
console.log('info'
console.log('info')
Close parenthesis.
JavaScript
switch(a){{ case 56: break; }}
switch(a){{ case 56: break; default: break; }}
Add default case.
Java
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
{{'status':'message'}}
{{"status":"message"}}
Use double quotes.
JSON
let val: number | null = null; val.toFixed(11);
let val: number | null = null; if(val!==null) val.toFixed(11);
Null check.
TypeScript
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
DELETE FROM orders WHERE email=39
DELETE FROM orders WHERE email=39;
Add semicolon.
SQL
Post.save();
Post.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
class User def method end end
class User def method end end
Correct.
Ruby
<?php // code ?>
<?php // code ?>
Correct.
PHP
let foo: i32 = "output";
let foo: &str = "output";
Type mismatch.
Rust
<person name='info'/>
<person name="info"/>
Double quotes.
XML
val y: Int = 'world'
val y: String = 'world'
Fix type.
Kotlin
items[62]
if (items.indices.contains(62)) items[62]
Check index.
Kotlin
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
if (temp = 72)
if (temp == 72)
Use ==.
R
echo world hello
echo 'world hello'
Quote to prevent splitting.
Shell
yield z
yield z
Correct yield.
Python
#header {{ color: #fff; }}
#header {{ color: #fff; }}
Correct.
CSS
if item = 57
if item == 57
Use ==.
Go
for (int i=0; i<45; i++) {{}}
for (int i=0; i<45; i++) {{}}
Correct.
Java
String name = 'world';
String name = 'world';
Correct.
Dart
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
["test", 52]
["test", 52]
Correct.
JSON
let val = 92;
let val = 92;
Correct.
JavaScript
print 'test'
print 'test';
Add semicolon.
Perl
function compute(index) print(index) end
function compute(index) print(index) end
Correct.
Lua
function baz(a:string){{return a;}} baz(15);
function baz(a:string){{return a;}} baz('world');
Pass correct type.
TypeScript
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
void main() {{ print('value') }}
void main() {{ print('value'); }}
Add semicolon.
Dart
class = 'test'
class_name = 'test'
'class' is a keyword.
Python
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
if num = 56
if num == 56
Use ==.
Ruby
function process() {{ echo 'value'; }}
function process() {{ echo 'value'; }}
Correct.
PHP
println('data')
println("data")
Double quotes.
Scala
{{'name':32, 'value' 90}}
{{'name':32, 'value':90}}
Colon missing.
Python
<br></br>
<br>
Self-closing.
HTML
id: test value: world,
id: test value: world
Remove comma.
YAML