5 js written test interview questions with answers

/**
 * 题目1: 解析Cookie字符串转化为对象
 * 输入:'foo=bar; equation=E%3Dmc%5E2'
 * 输出:{ foo: 'bar', equation: 'E=mc^2' }
 * 测试: parseCookie('foo=bar; equation=E%3Dmc%5E2')
 */
function parseCookie(str) {
    
    

} 
/**
 * 题目2: 找出对象中符合要求的项
 * 输入: 原始对象:{ a: 1, b: '2', c: 3 }, 筛选条件:x => typeof x === 'string'
 * 输出:{ 'b': '2' }
 * 测试: pickBy({ a: 1, b: '2', c: 3 }, x => typeof x === 'string')
 */
function pickBy(obj, fn) {
    
    

}
/**
 * 题目3: 字符串中划线转驼峰式
 * 输入: 'this-is-a-selector'
 * 输出: 'thisIsASelector'
 * 测试: camelize('this-is-a-selector') === 'thisIsASelector'
 */
/**
 * 
 */
function camelize(str) {
    
    

}

/**
 * 题目4:合并多个对象
 * 输入:{ a: [1,2,3], b: { name: 'b'} }, { a: [4], b: { age: 18 } }
 * 输出:{ a: [1,2,3,4], b:{name:'b', age: 18}}
 * 测试: merge({ a: [1,2,3], b: { name: 'b'} }, { a: [4], b: { age: 18 } })
 */
function merge() {
    
    

}
/**
 * 题目5:数组去重
 * 输入: [1, 2, 1, 3, 4, 1, 2]
 * 输出: [1,2,3,4]
 * 测试: handle([1, 2, 1, 3, 4, 1, 2])
 */
const handle = (arr) => {
    
    

}

Answer

1.

function parseCookie(cookieString) {
    
    
  const cookieArray = cookieString.split('; ');
  const cookieObject = {
    
    };

  for (const cookie of cookieArray) {
    
    
    const [key, value] = cookie.split('=');
    cookieObject[key] = decodeURIComponent(value);
  }

  return cookieObject;
}

// 测试
const result = parseCookie('foo=bar; equation=E%3Dmc%5E2');
console.log(result); // 输出: { foo: 'bar', equation: 'E=mc^2' }

This function first splits the input Cookie string according to semicolons and spaces, and then stores the results in an array. Next, we iterate through each cookie in the array, split it into a key and a value by the equal sign, and store the decoded value in an object. Finally, the object is returned.

2.

function pickBy(obj, condition) {
    
    
  const result = {
    
    };

  for (const key in obj) {
    
    
    if (condition(obj[key])) {
    
    
      result[key] = obj[key];
    }
  }

  return result;
}

// 测试
const inputObj = {
    
     a: 1, b: '2', c: 3 };
const conditionFunc = x => typeof x === 'string';
const outputObj = pickBy(inputObj, conditionFunc);

console.log(outputObj); // 输出:{ 'b': '2' }

This function first creates an empty objectresult to store key-value pairs that meet the conditions. Then, it iterates through all the keys of the input object and checks whether the value corresponding to each key satisfies the condition. If the condition is met, the key-value pair is added to the result object. Finally, the result object is returned.

3.

function camelize(str) {
    
    
  return str
    .split('-')
    .map((word, index) =>
      index === 0 ? word : word[0].toUpperCase() + word.slice(1)
    )
    .join('');
}

// 测试
console.log(camelize('this-is-a-selector')); // 输出: 'thisIsASelector'

This function first uses the split() method to split the input stringstr into an array of words to - as separator. Then, use the map() method to iterate through each word in the array, leaving the first word unchanged and capitalizing the first letter of the other words. Finally, use the join() method to concatenate the processed word arrays into a string.

4.

function merge(obj1, obj2) {
    
    
  const result = {
    
     ...obj1, ...obj2 };

  for (const key in result) {
    
    
    if (Array.isArray(obj1[key]) && Array.isArray(obj2[key])) {
    
    
      result[key] = [...obj1[key], ...obj2[key]];
    } else if (typeof obj1[key] === 'object' && typeof obj2[key] === 'object') {
    
    
      result[key] = merge(obj1[key], obj2[key]);
    }
  }

  return result;
}

const obj1 = {
    
     a: [1, 2, 3], b: {
    
     name: 'b' } };
const obj2 = {
    
     a: [4], b: {
    
     age: 18 } };
const mergedObj = merge(obj1, obj2);

console.log(mergedObj); // 输出: { a: [1, 2, 3, 4], b: { name: 'b', age: 18 } }

This function first merges the two objects into a new object, and then iterates over the keys of the new object. If the key's value is an array, it merges the two arrays into a new array. If the value of the key is an object, it will call the merge function recursively to merge the two objects. Finally, the merged object is returned.

5.

function handle(arr) {
    
    
  const uniqueArray = [...new Set(arr)];
  return uniqueArray;
}

const inputArray = [1, 2, 1, 3, 4, 1, 2];
const outputArray = handle(inputArray);
console.log(outputArray); // [1, 2, 3, 4]

This function uses JavaScript's Set data structure to remove duplicates and then convert it back to an array.

Guess you like

Origin blog.csdn.net/qq_37215621/article/details/133877413