0% found this document useful (0 votes)
13 views

Challenge

The document contains three code snippets that provide solutions to different problems: 1) A function that checks if a word can be found on a game board. 2) A function that returns the most frequent days of the week for a given year. 3) A function that replaces instances of 'you' or similar words with 'your client' in a given string.

Uploaded by

Hammad ul Aziz
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Challenge

The document contains three code snippets that provide solutions to different problems: 1) A function that checks if a word can be found on a game board. 2) A function that returns the most frequent days of the week for a given year. 3) A function that replaces instances of 'you' or similar words with 'your client' in a given string.

Uploaded by

Hammad ul Aziz
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

challenge

NO.3

const autocorrect = input => {


//let a='You u youville utube you youyouyou uuu raiyou united youuuu u you';
console.log(input);
console.log('||');
let regex=/(\byou\b|\byou+u\b|\bu\b)/gi
console.log(input.replace(regex, "your client"));

return input.replace(regex, "your client");

};

NO.2

const mostFrequentDays = year => {


let dayName = function (d){
return d.toLocaleString('en',{weekday:'long'})
};
// 1 Jan
let d = new Date(year, 0, 1);
let d0 = dayName(d);
// 31 Dec
let d1 = dayName(new Date(year, 11, 31));
let result = d0 == d1? [d0] : [d0, d1];
// If d0 is Sunday, reverse result
return d.getDay()? result : result.reverse();
};

NO.1

function checkWord( board, word ) {

let queue = board.reduce((acc, row, i) => {


row.forEach((x, j) => {
if (x === word[0]) {
acc.push ( { pos: {r: i, c: j} , nextIndex: 1, path: [board[0].length*i
+ j ] } );
}
});
return acc;
}, []);
let exploreWord = (obj, queue) => {
let allMoves = [ {r: obj.pos.r - 1, c: obj.pos.c },
{r: obj.pos.r + 1, c: obj.pos.c },
{r: obj.pos.r, c: obj.pos.c - 1 },
{r: obj.pos.r, c: obj.pos.c + 1 },
{r: obj.pos.r - 1, c: obj.pos.c - 1 },
{r: obj.pos.r - 1, c: obj.pos.c + 1 },
{r: obj.pos.r + 1, c: obj.pos.c - 1 },
{r: obj.pos.r + 1, c: obj.pos.c + 1 }];
allMoves.forEach((o) => {
let index = board[0].length * o.r + o.c;
if (o.r >= 0 && o.r < board.length && o.c >= 0 && o.c < board[0].length) {
if (board[o.r][o.c] === word[obj.nextIndex] && !
obj.path.includes(index)) {
let cloneObj = JSON.parse(JSON.stringify(obj));
cloneObj.pos = { r: o.r, c: o.c };
cloneObj.nextIndex += 1;
cloneObj.path.push(index);
queue.push(cloneObj);
}
}
});
};
while (queue.length > 0) {
let obj = queue.shift();
if (obj.nextIndex === word.length) {
return true;
}
exploreWord(obj, queue);
}
return false;

var testBoard = [
["E","A","R","A"],
["N","L","E","C"],
["I","A","I","S"],
["B","Y","O","R"]
];

checkWord( testBoard, "EAR" )

You might also like