Skip to content

Instantly share code, notes, and snippets.

@potatowave
Forked from kvirani/print-in-frame.js
Last active October 6, 2016 03:37
Show Gist options
  • Save potatowave/3328ff8d23a9992974a03d1bc2ac1489 to your computer and use it in GitHub Desktop.
Save potatowave/3328ff8d23a9992974a03d1bc2ac1489 to your computer and use it in GitHub Desktop.
W1D2 - Debugging incorrect code
function printInFrame(list) {
var list = list.split(' ');
var longest = longestStr(list).length;
var border = repeat('*', longest + 2);
console.log(border);
for (var word of list) {
console.log('* ' + word + repeat(' ', longest - word.length - 1) + '*');
}
console.log(border);
}
function repeat(str, times) {
var result = str;
for (var i = 0; i <= times; i++) {
result += str;
}
return result;
}
function longestStr(list) {
var longest = list[0];
for (var str of list) {
if (str.length > longest.length) {
longest = str;
}
}
return longest;
}
// Test driver code, do not modify
printInFrame('May the force be with you');
printInFrame('Here\'s Johnny!');
printInFrame('Supercalifragalisticexpialadocious');
printInFrame('Lost, like tears in the rain');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment