Uber Interview Question for Front-end Software Engineers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
0
of 0 vote

<div id="sample">Sample</div>
<script>
function addClass(id){
var elt = document.getElementById(id);
for(let i =1 ; i<arguments.length ; i++){
elt.classList.add(arguments[i]);
}}
addClass('sample' , 'sample','sample_1', 'x');
</script>

- Pradanya February 06, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can achieve this by using below code

<div id="sample">Sample</div>
<script>
function addClass(id){
var elt = document.getElementById(id);
for(let i =1 ; i<arguments.length ; i++){
elt.classList.add(arguments[i]);
}}
addClass('sample' , 'sample','sample_1', 'x');
</script>
Here you are using arguments which is an Array-like object accessible inside functions that contains the values of the arguments passed to that function. And classList.add to add class and classList.remove to remove class.
The Element.classList is a read-only property that returns a live DOMTokenList collection of the class attributes of the element.
Using classList is a convenient alternative to accessing an element's list of classes as a space-delimited string via element.className.

- pradnyavetale February 06, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const addClass = (el, cls) => {
    let classes = el.className.split(/\s+/).reduce((classes, cls) => {
        classes[cls] = true;
        return classes;
    }, {});

    if (!(cls in classes)) {
        el.className = Object.keys(classes).join(' ') + ' ' + cls;
    }
};

const removeClass = (el, cls) => {
    let classes = el.className.split(/\s+/).reduce((classes, cls) => {
        classes[cls] = true;
        return classes;
    }, {});

    if (cls in classes) {
        delete classes[cls];
        el.className = Object.keys(classes).join(' ');
    }
}

- blacklight86 August 27, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const myJquery = (el) => ({
	addClass: (className) => {
		el.classList.add(className);
		return myJquery(el);
	},
	removeClass: (className) => {
		el.classList.remove(className);
		return myJquery(el);
	}
});

const $ = (el) => myJquery(document.querySelector(el));

$('#container').addClass('blue').addClass('green').removeClass('blue');

- aabramya December 05, 2019 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More