Specifically, this references JavaScript, a programming language that happens to be one you can run on StackExchange/StackOverflow. However, you may use any programming language to do this challenge. This is not actual JS golfing, but merely a "code-golfable" challenge. Actual complete parsing of JS is not needed.
An example of JS looks like this (try running all the snippets in here):
const constant = 1
var value = 2
for (let i = 0; i < 21; i++) {
value += constant + value // Addition
}
// Print the value
document.write(value)
Problem is, I want my code size to be a bit smaller. So, I could rename the variables to make it look like this:
var a = 1
var b = 2
for (let i = 0; i < 21; i++) {
b += a + b // Addition
}
// Print the value
document.write(b)
Notice how the variables have been changed to one letter, and the const
keyword has been shortened to var
. Then, we can remove most spaces and all comments:
var a=1
var b=2
for (let i = 0; i < 21; i++) {
b+=a+b
}
document.write(b)
So, here are the rules (look at the test case; it will make more sense):
- The keywords
const
,let
,var
,while
, andfor
are not variables (notice the spaces!) and should not be changed, with the exception ofconst
, which should be changed tovar
for shortening. - A variable is defined as the longest string of characters that contain only the characters
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$
, not including the rule above. Furthermore, a variable is a variable if and only if one variable is preceded withconst
,let
, orvar
(with spaces). - All variables with the same name must be renamed into a single character variable (one of
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$
). All variables with different names must have different values in the result. You may assume that there are no more than 50 unique variables, so only one character is required. - A single-line comment is all the characters at or after
//
- All single-line comments should be removed.
- All spaces that are not within keywords must be removed. (Not newlines!)
- After all of this, remove any empty lines. This includes comment removal.
- Compact numbers! If a number is an integer, it is defined a number, but with a
.
and all zeros at the end. - If an integer has more than two zeros, write the number before the final zeros (before the dot), an
e
, then the number of zeros (30000
to3e4
,1515000
to1515e3
). This is optional for two zeros. While this is not complete, it would be too complicated to compact numbers well. - You should output all other characters normally.
There is only one test case, but it is a strange one!
Test case:
// Hello world, but stranger!
// funny spacing
const m = 72000.00 / 1000
var h = String.fromCharCode(m)
let string = " world!" // Value to print
var l
for (let i = 1.00000; i < string.length; i++) {
// Number prototype to throw off some lazy variable detection
Number
console.log(string[i]) // Print each value
l += string[i]
}
string = string.split("").join("<br>")
h = l.toUpperCase()
document.write("<!DOCTYPE html><i>" + h.toUpperCase() + " " + string + "</i>")
Result:
var m=72e3/1e3
var h=String.fromCharCode(m)
let s=" world!"
var l
for(let i=1;i<s.length;i++){
Number
console.log(s[i])
l+=s[i]
}
s=s.split("").join("<br>")
h=l.toUpperCase()
document.write("<!DOCTYPE html><i>"+h.toUpperCase()+" "+s+"</i>")
Notice Number
! This is code-golf, so the shortest answer in bytes wins.
0123456789
? And would it be a valid assumption that all long variable names don't start with a digit? \$\endgroup\$const
change tovar
or not? (first rule says no, first example says yes) \$\endgroup\$for (let i = 0; i < 21; i++) {
in the final iteration of the examplefor(let i=0;i<21;i++){
(spaces removed, except afterlet
)? \$\endgroup\$