Port a Go Ledger
Lesson 47 of 48 in Coddy's JavaScript for Go Developers course.
Challenge
EasyPort the complete Go reference below. Input is one JSON array of [name, quantity] pairs. Names contain only ASCII characters and quantities are integers. Sum every update by name, then retain only positive totals. Sort names by ASCII order and return [name, total] pairs. Use a JavaScript Map so names such as __proto__ remain ordinary data. All input numbers and intermediate integer totals stay within JavaScript's safe integer range.
The Go reference decodes quantities into int64 without passing through float64, accumulates into a map, sorts keys explicitly, and encodes an allocated empty slice as [] rather than null. JavaScript Map supplies the data-key behavior, while Number is exact within the stated safe-integer bound. Map iteration order differs between the languages, so retain the explicit final sorting. The locked JavaScript driver owns JSON decoding and encoding.
package main
import (
"encoding/json"
"os"
"sort"
)
// Inputs and every intermediate total stay within JavaScript's safe integer range.
func solve(rows [][]json.RawMessage) [][]any {
totals := make(map[string]int64)
for _, row := range rows {
var name string
var quantity int64
if err := json.Unmarshal(row[0], &name); err != nil { panic(err) }
if err := json.Unmarshal(row[1], &quantity); err != nil { panic(err) }
totals[name] += quantity
}
names := make([]string, 0, len(totals))
for name, total := range totals {
if total > 0 { names = append(names, name) }
}
sort.Strings(names)
result := make([][]any, 0, len(names))
for _, name := range names { result = append(result, []any{name, totals[name]}) }
return result
}
func main() {
var rows [][]json.RawMessage
if err := json.NewDecoder(os.Stdin).Decode(&rows); err != nil { panic(err) }
encoder := json.NewEncoder(os.Stdout)
encoder.SetEscapeHTML(false)
if err := encoder.Encode(solve(rows)); err != nil { panic(err) }
}Edit exercise.js and keep its exported solve(input) API. The visible locked driver.js reads JSON and prints the return value as compact JSON followed by a newline. Do not add diagnostic output.
Try it yourself
import { readFileSync } from "node:fs";
import { solve } from "./exercise.js";
// One JSON value arrives on stdin. Return data; do not print inside solve.
const input = JSON.parse(readFileSync(0,"utf8"));
console.log(JSON.stringify(solve(input)));
All lessons in JavaScript for Go Developers
1First JavaScript
Run JavaScriptFormatting and TemplatesDeclarations and RebindingInteger Types and NumberNil and Two Absence ValuesRecap: Receipt4Iteration Pipelines
For Conditions and WhileLoops and Array CallbacksZero Values and IdentitiesOrdering and Comparator SignsRecap: Build a Ranking2Decisions and Flow
Comparable Values and IdentityBool Conditions and TruthinessIf and Conditional ValuesRange Versus For-ofRecap: Shipping Rules3Collections
Slices and Array ObjectsStruct Fields and Own KeysMap, Set, and Key PresenceBacking Arrays and SpreadRecap: Inventory Counts6Errors and Modules
Encoding/json and Dynamic DataError Values Versus ExceptionsPackage Exports and ESMType Assertions and ChecksRecap: Validate a Batch9Project: Order Processor
Model an OrderApply Member PricingPractice on your own: Online JavaScript compiler