JSFiddle - React, Tailwind, and code Playground

by Emily Humphrey

JavaScript

let rods = [6, 5, 4, 4, 2, 2, 8];

function rodOffcut(lengths) {
	let rods = lengths;
	const rodLengths = [];
	console.log("called")
	
	
	do {
		rods = rodOffcut(rods);
		console.log(rods.length)
	}
	while (rods.length > 0)
}

function cutRods(rods){
	const rodAmount = rods.length;
	const sortedRods = rods.sort( ( a,b ) => a - b );
	const shortestRod = sortedRods.splice(0,1)[0];
	let assembledRods = sortedRods. filter( x => x - shortestRod );
	return assembledRods.map(x => x - shortestRod);
}

rodOffcut(rods);