۱۲ مهر ۱۳۹۹

سوالهای جاوااسکریپتی مصاحبه‌ی فرانت اند

کدام اسب سریع‌تر می‌رسد؟ :)

 

مورد اول

setTimeout(() => console.log("Horse A"), 1000)
console.log("Horse B")

مورد دوم

setTimeout(() => console.log("Horse A"), 0)
console.log("Horse B")

مورد سوم

setTimeout(() => console.log("Horse A"), 0);
wait60Se(); //expensive sync computation
console.log("Horse B")

مورد چهارم

const operation = fetch('http://something.com');
operation.then(() => console.log("Horse A"));
console.log("Horse B")

مورد پنجم

setTimeout(() => console.log("Horse A"), 0);
const promise = Promise.resolve(); 
promise.then(() => console.log("Horse B"));

مورد ششم

setTimeout(() => console.log("Horse A"), 5)
const promise = new Promise((resolve) => {
   setTimeout(() => resolve(), 10) 
})
promise.then(() => console.log("Horse B"))

مورد هفتم

setTimeout(() => console.log("Horse A"), 0);
const promise = fetch("someUrl") // takes 100ms
promise.then(x => console.log("Horse B"));
waitFor200ms();

منابع:


رفع مشکل سایدبار

https://codepen.io/mberneti/pen/eYgwreg

 

Select all p elements which have a "lang" attribute equal to "en", 
and are the direct child of "div" elements with CSS class "post"?

 

 

How would you improve the following code?
const orders = [500, 30, 99, 15, 223];
const total = 0;
const withTax = [];
const highValue = []; // is more than 100

// > const total = orders.reduce((acc, cur) => acc + cur);
// > const withTax = orders.map(v => v * 1.1);
// > const highValue = orders.filter(v => v > 100);

 

 

Given a sorted array and a number x, find a pair in array whose sum is closest to x.
Examples:
Input: arr[] = {10, 22, 28, 29, 30, 40}, x = 54
Output: 22 and 30
Input: arr[] = {1, 3, 4, 7, 10}, x = 15
Output: 4 and 10

 

Improve findByUserId with changing data structure:
user = [{userId:"asd",name:"bernet"},{userId:"ahwuiq",name:"mohammadreza"}]

پاسخ‌ها:
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

همه‌ی موارد بجز مورد ششم اسب B  اول میشه.

 

withTax = orders.map((item) => {
   total += orders[i];
		return item * 1.1;
});

for (i = 0; i < orders.length; i++) {
   total += orders[i];

   withTax.push(orders[i] * 1.1);

   if (orders[i] > 100) {
      highValue.push(orders[i]);
   }
}

 

 

 

let diff = Infinity;

let l = 0;
let r = arr.length - l;

while() {

 let currentDiff = x - arr[l] + arr[r];
  if(currentDiff < diff)
  {
  	diff = currentDiff;
  }
   
  if(x > arr[l] + arr[r])
  {
  	l++;
  }
  else {
  	r--;
  }

}

فهرست مطالب `جاوااسکریپت`

نظرات خوانندگان این نوشته

Avatar of Jonathan Reinink

صالح

23 اردیبهشت 1400

همشون جالب بودن، دمت گرم 🤘❤️

نظری در این مورد دارید؟ خوشحال می‌شم اون رو برام ارسال کنید.

captcha