wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
for (data in data)
for (data of data)
for...in iterates keys.
JavaScript
if item = 22
if item == 22
Use ==.
Ruby
cin >> c cout << c;
cin >> c; cout << c;
Add semicolon.
C++
values(65)
if length(values) >= 65, values(65), end
Check length.
MATLAB
const http = require('http'); http.createServer((req,res) => res.end('message')).listen(4);
const http = require('http'); http.createServer((req,res) => res.end('message')).listen(4);
Correct.
Node.js
class Product {{ int c; }} obj.c=5;
class Product {{ public int c; }} obj.c=5;
Make field public.
Java
values[23]
if (length(values) >= 23) values[23]
Check length.
R
let data = 'output'
let data = "output"
Double quotes.
Swift
b > 84 & b < 66
b > 84 and b < 66
Use 'and' not '&'.
Python
else print('world')
else: print('world')
Colon after else.
Python
<table><tr><td>hello<td>world</tr></table>
<table><tr><td>hello</td><td>world</td></tr></table>
Close td.
HTML
<div color=red>
<div style='color:red;'>
Use style attribute.
CSS
try {{ throw 'test'; }} catch(e) {{}}
try {{ throw new Error('test'); }} catch(e) {{}}
Throw Error objects.
JavaScript
let count: Int = 'hello'
let count: String = 'hello'
Fix type.
Swift
<user><desc>value</desc><age>13</age></user
<user><desc>value</desc><age>13</age></user>
Add closing >.
XML
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
var x = 51;
var x = 51;
Correct.
Dart
while read line; do echo $line; done < input.csv
while read line; do echo $line; done < input.csv
Correct.
Shell
<input type='text' value='hello'>
<input type='text' value='hello' name='id'>
Add name attribute.
HTML
raise 'message'
raise Exception('message')
Raise needs an exception class.
Python
print 'output'
print 'output';
Add semicolon.
Perl
if (b = 21) {{}}
if (b === 21) {{}}
Use === for equality.
JavaScript
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
if b = 94 then print('value') end
if b == 94 then print('value') end
Use ==.
Lua
bar
bar()
Add parentheses.
Swift
class User {{ int result; }};
class User {{ public: int result; }};
Make public.
C++
my @arr = (41,23,1);
my @arr = (41,23,1);
Correct.
Perl
86z = 10
z86 = 10
Variable cannot start with digit.
Python
let x = 96; x += 1;
let mut x = 96; x += 1;
Need mut to modify.
Rust
if (count = 61)
if (count == 61)
Use ==.
Scala
test
test()
Add parentheses.
Kotlin
function handle() {{ echo 'hello'; }}
function handle() {{ echo 'hello'; }}
Correct.
PHP
let temp: i32 = "world";
let temp: &str = "world";
Type mismatch.
Rust
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
<ul><li>hello<li>world</ul>
<ul><li>hello</li><li>world</li></ul>
Close li.
HTML
def bar(result): return result + 1
def bar(result): return result + 1
Correct.
Python
if temp > 27 print('value')
if temp > 27: print('value')
Colon missing after if.
Python
if (index = 81) {}
if (index == 81) {}
Use ==.
Dart
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
if ($y = 85) {{}}
if ($y -eq 85) {{}}
Use -eq.
PowerShell
let count = 12;
let count = 12;
Correct.
JavaScript
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
if (x = 70) {{}}
if (x == 70) {{}}
Use ==.
Kotlin
for num in range(89) print(num)
for num in range(89): print(num)
Colon after for.
Python
{{"id":"hello" "status":84}}
{{"id":"hello", "status":84}}
Add comma.
JSON
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
fn compute() -> i32 {{ 80 }}
fn compute() -> i32 {{ 80 }}
Correct.
Rust
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
def foo(): print('message')
def foo(): print('message')
Indent function body.
Python
<center>hello</center>
<div style='text-align:center;'>hello</div>
Use CSS.
HTML
// comment
/* comment */
Use /* */.
CSS
JOIN orders ON orders.id = orders.id
JOIN orders ON orders.id = orders.id
Correct.
SQL
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('message')); app.listen(60);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('message')); app.listen(60, () => console.log('listening'));
Add callback.
Node.js
while foo > 12 foo -= 1
while foo > 12: foo -= 1
Colon missing after while.
Python
let y = 12; let y = 79;
let y = 12; y = 79;
Duplicate declaration.
JavaScript
list[56]
if (list.indices.contains(56)) list[56]
Check index.
Kotlin
[48, 32, 8
[48, 32, 8]
Close bracket.
Python
object Item {{ def main(args: Array[String]) = println("message") }}
object Item {{ def main(args: Array[String]): Unit = println("message") }}
Add return type Unit.
Scala
<user name='data'/>
<user name="data"/>
Double quotes.
XML
switch(data){{ case 43: break; }}
switch(data){{ case 43: break; default: break; }}
Add default case.
Java
name: world age: 23
name: world age: 23
Correct.
YAML
with open('data.txt') as file_handle: data = file_handle.read()
with open('data.txt') as file_handle: data = file_handle.read()
Correct.
Python
SELECT COUNT(*) FROM users
SELECT COUNT(*) FROM users;
Missing semicolon.
SQL
String name = 'test';
String name = 'test';
Correct.
Dart
class = 'message'
class_name = 'message'
'class' is a keyword.
Python
{{'name':56, 'title' 42}}
{{'name':56, 'title':42}}
Colon missing.
Python
if (a = 33)
if (a == 33)
Use ==.
C++
print 'hello'
print('hello')
Parentheses for function call.
Lua
DELETE FROM items WHERE status=93
DELETE FROM items WHERE status=93;
Add semicolon.
SQL
h1 {{ font-size:27px color:#333; }}
h1 {{ font-size:27px; color:#333; }}
Add semicolon.
CSS
class Product def method end end
class Product def method end end
Correct.
Ruby
fs.readFile('input.csv', (err,data) => {{ if(err) throw err; }});
fs.readFile('input.csv', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
SELECT id role FROM items;
SELECT id, role FROM items;
Add comma.
SQL
'result' + 34
'result' + 34.to_s
Convert int.
Ruby
function baz() {{ return {{key:'message'}} }}
function baz() {{ return {{key:'message'}}; }}
Return object on same line.
JavaScript
echo 'result'
echo 'result';
Add semicolon.
PHP
<div><p>data</div></p>
<div><p>data</p></div>
Nest properly.
HTML
Write-Host 'result'
Write-Host 'result'
Correct.
PowerShell
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
fmt.Println 'result'
fmt.Println('result')
Missing parentheses.
Go
if item = 57:
if item == 57:
Use == for comparison.
Python
if z = 50
if z == 50
Use ==.
Go
Post.save();
Post.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
<a href='https://demo.net' target='_blank'>
<a href='https://demo.net' target='_blank' rel='noopener'>
Add rel for security.
HTML
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
if [ $a = 40 ]; then
if [ "$a" = 40 ]; then
Quote variable.
Shell
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
.User {{ color: blue; }}
.User {{ color: blue; }}
Correct.
CSS
let mut data=22; let r1=&mut data; let r2=&mut data;
let mut data=22; {{ let r1=&mut data; }} let r2=&mut data;
Only one mutable borrow.
Rust
int list[58]; list[58]=5;
int list[58]; if(58<58){{}} else list[58]=5;
Bounds check.
C++
function foo(): void {{ return 48; }}
function foo(): number {{ return 48; }}
Return type mismatch.
TypeScript
for (int i=0; i<90; i++) {{}}
for (int i=0; i<90; i++) {{}}
Correct.
Java
List(36,43,22)
List(36,43,22)
Correct.
Scala
$items[75] = 5;
if (isset($items[75])) $items[75] = 5;
Check existence.
PHP
let text1 = String::from("hello"); let s2 = text1; println!("{{}}", text1);
let text1 = String::from("hello"); let s2 = text1.clone(); println!("{{}}", text1);
Clone to avoid move.
Rust
match index {{ 1 => {{}} }}
match index {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust