Interview JS Test

HTML

View the console.

JavaScript

console.log("Test 1 -----");
// Create a function that accepts one object parameter.  
// If the object has the property ‘selected’ set to a true value, 
// delete the property ‘selected’ from the object, otherwise set 
// selected to true.

var obje = function(obj){
	if(typeof obj !== "object"){
        console.log("this is a '"+  typeof obj + "' not an butt nugget.");
        return false;
    }
    if(obj["selected"]){
        console.log("`selected` is true, therefore deleting it");
    	delete obj["selected"];
	} else {
        console.log("setting `selected` to true.");
    	obj["selected"] = true;
    }
};
// `selected` will be true
obje( {"selected": true});
obje( {"selected": "this should be true"});
obje( {"selected": 1});
obje( {"selected":999});

// `selected` will be set to true
obje( {"selected": false});
obje( {"lkjlkj": 0});
obje( {});
obje( []);
obje( ["a","b"]);

// not an object
obje( "hey!");
obje( false);
obje( true);


console.log("\r\n\r\nTest 2 -----");
// Create a for loop that will iterate 10 times. 
// If the iterator is an even number it should write 
// to the console and say ‘2 is even’ or 
// ‘1 is odd’; 2 and 1 being the iterator value.
var type = "odd";
for(i=1;i<11;i++){
	console.log(i + " is " + type);
	type = (type=="even") ? "odd" : "even";  
}

console.log("\r\n\r\nTest 3 -----");
//check if a parameter is all letters
var func = function(p1){
	if(p1.match("[^A-Za-z]")){
        console.log("some non-letter found!");
		return false;
	} else {
        console.log("only letters found!");
		return true;
	}
};

// all letters
func("aaaBRGSfgkYUjKUkjKUTF");
func("b");
func("");

// contains non-letters
func("aaaaaa4aaaaa");
func("#");
func("aaaaaaaåaaaa");
func("aaaaa,aaaaaa");
func("aaaaa aaaaaa");