Skip to content

Instantly share code, notes, and snippets.

@shankarcabus
Last active December 23, 2015 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shankarcabus/3f9d4b884a2c85f0f4a3 to your computer and use it in GitHub Desktop.
Save shankarcabus/3f9d4b884a2c85f0f4a3 to your computer and use it in GitHub Desktop.
// how could you rewrite the following to make it shorter?
if (foo) {
bar.doSomething(el);
} else {
bar.doSomethingElse(el);
}
// how could this code be rewritten in order to remove the if?
if (foo) {
bar.doSomething();
}
// given the following code, and assuming that each created object has a
// 'destroy' method, how would you destroy all of the objects contained in the
// myObjects object?
var myObjects = {
thinger : new myApp.Thinger(),
gizmo : new myApp.Gizmo(),
widget : new myApp.Widget()
};
// how could you improve the following code?
$(document).ready(function() {
$('.foo #bar').css('color', 'red');
$('.foo #bar').css('border', '1px solid blue');
$('.foo #bar').text('new text!');
$('.foo #bar').removeClass('btn');
$('.foo #bar').removeClass('primary');
$('.foo #bar').click(function() {
$(this).attr('title', 'new title');
$(this).width('100px');
});
$('.foo #bar').click();
});
// how would you improve the following code?
for (i = 0; i <= 100; i++) {
$('#thinger').append('<p><span class="thinger">i am thinger ' + i + '</span></p>');
$('#gizmo').append('<p><span class="gizmo">i am gizmo ' + i + '</span></p>');
}
/*
According to the following HTML:
<div id="button">button</div>
<button id="alert">Click me!</button>
How to improve and make the code below work?
*/
$("#button").click(function(){
$("body").append('<button class="btn" id="alert">Click now!</button>');
});
$("#alert").click(function(){
alert('Alert clicked');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment