During a recent job interview, I was given a programming test / quiz – which I botched up royally – I froze. And no, I didn’t get the job. So in the spirit of my math teachers – I’m going to ‘Show My Work’.
The test was to write the code to display numbers 1-100, if divisible by 3 – write “Yeah”, if divisible by 5 – write “Baby”, if divisible by both – “Yeah Baby”. I didn’t freeze because I didn’t know how to do the “for/while” statements/loop or the “if” statements, it was because I had never had to check for a WHOLE number before. So my mind was racing on a small detail of the question – in my mind an important part of the question (but not to them I guess). Well, I now know how to check for a whole number in 3 different ways. Something learned!!!
So here are my 3 versions (I have a tendency to be dramatic you know):
JavaScript:
<script type="text/javascript">
function YB(){
for(i=1;i<=100;i++)
{
x = i/3;
y = i/5;
if(x == parseInt(x)){
a = " Yeah";
}
if (y == parseInt(y)){
a = a+ " Baby";
}
document.write(i+" "+a+" <br />");
a = "";
}
}
</script>
PHP:
for ($i =1; $i<=100; $i++)
{
if($i % 3 == 0){
$a = " Yeah";
}
if($i % 5 == 0){
$a .= " Baby";
}
print $i." ".$a."<br />";
$a = "";
}
Java:
public class yeahBaby{
public static void main(String[] args) {
for(double i =1; i <= 100; i=i+1)
{
String a = "";
double x = i/3;
double y = i/5;
if(x == Math.rint(x)){
a = " Yeah";
}
if(y == Math.rint(y)){
a = a+ " Baby";
}
System.out.println(Math.round(i)+" "+a);
}
}
}