-
-
Save potatowave/3328ff8d23a9992974a03d1bc2ac1489 to your computer and use it in GitHub Desktop.
W1D2 - Debugging incorrect code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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