Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/test1.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
function hello1(name) {
if(name){
if (name) {
return 'hello ' + name
}
return 'Who are you?'
}

function hello2(firstname, lastname) {
// TODO: Write code here
if (firstname) {
return `My name is ${firstname}${lastname ? ` ${lastname}` : ''}.`;
} else {
return `I have no firstname, but my lastname is ${lastname}.`;
}
}

module.exports = { hello1, hello2 };
13 changes: 9 additions & 4 deletions src/test2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ function multipleHello(number) {
}

function multipleTextByCharacter(text, selectedChar) {
/**
* count character in given text then multiple the text by that number
*/
// TODO: Write code here
/**
* count character in given text then multiple the text by that number
*/
// TODO: Write code here
let txt = "";
for (let i = 0; i < (text.match(new RegExp(selectedChar, "g")) || []).length; i++) {
txt += text;
}
return txt;
}


Expand Down
6 changes: 5 additions & 1 deletion src/test3.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ function spaceHello(number) {
const hello = "hello";
let text = "";
for (let i = 0; i < hello.length; i++) {
text += hello[i] + space;
if (i === 0) {
text += hello[i];
} else {
text += space + hello[i];
}
}
return text;
}
Expand Down
19 changes: 19 additions & 0 deletions src/test4.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,39 @@ function sumAbs(array) {
* หาผลบวกของค่า absolute ของตัวเลขใน array
*/
// TODO: Write code here
let res = 0;
for (const i of array) {
res += Math.abs(i);
}
return res;
}

function sumPositive(array) {
/**
* เลือกเฉพาะจำนวนบวกมาหาผลรวม ตอบผลรวมนั้น
*/
// TODO: Write code here
let res = 0;
for (const i of array) {
if (i >= 0) {
res += i;
}
}
return res;
}

function maxAbsSum(array, maxNumber) {
/**
* เลือกเลขใน array มาไม่เกิน maxNumber ตัว ที่เอามา absolute แล้วได้ผลบวกเยอะสุด ตอบผลบวกนั้น
*/
// TODO: Write code here
const arr = array.map(e => Math.abs(e));
arr.sort((a, b) => b - a);
let res = 0;
for (let i = 0; i < maxNumber; i++) {
res += arr[i];
}
return res;
}

module.exports = { sumSquare, sumAbs, maxAbsSum, sumPositive };
7 changes: 7 additions & 0 deletions src/test5.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function checkPrime(number) {
// TODO: write code here
let count = 0;
for (let i = 1; i <= number; i++) {
if (number % i === 0) {
count += 1;
}
}
return count === 2 ? true : false;
}

module.exports = { checkPrime };
Loading