Skip to content

Instantly share code, notes, and snippets.

@rbutera
Created January 11, 2024 16:58
Show Gist options
  • Select an option

  • Save rbutera/ae380adb5de87e02bfbe86892c518f2d to your computer and use it in GitHub Desktop.

Select an option

Save rbutera/ae380adb5de87e02bfbe86892c518f2d to your computer and use it in GitHub Desktop.
mario.c for cs50 except in javascript
const input = 1;
function makeLine(blocks, maxBlocks) {
const numSpaces = maxBlocks - blocks;
let output = ``
const addSpace = () => { output = output + ' '}
const addPadding = () => {
for(let i = 0; i < numSpaces; i++) {
addSpace()
}
}
const addBlocks = () => {
for(let j = 0; j < blocks; j++) {
output = output + '#'
}
}
addPadding();
addBlocks()
addSpace()
addBlocks()
addPadding();
output += '\n'
return output
}
function line(blocks, current) {
if(blocks > input) {
return current;
}
const newLine = makeLine(blocks, input)
return line(blocks + 1, current + newLine);
}
function validateInput() {
if (!input) {
throw new Error(`missing input`)
}
if (input < 1) {
throw new Error('please provide a size greater than 1')
}
if(typeof input !== 'number'){
throw new Error(`please provide size as a number between 1 and infinity`)
}
}
console.log(`creating mario pyramid of size ${input}`)
console.log(line(0, ``))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment