Skip to content

Commit

Permalink
Merge pull request #5 from yko-git/feature-001_002_003
Browse files Browse the repository at this point in the history
fix 001 002 003(feature-001_002_003)
  • Loading branch information
yko-git committed Mar 19, 2024
2 parents f69020b + 5d3961f commit decb049
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 64 deletions.
1 change: 0 additions & 1 deletion 001_syntax/006_object.js
Expand Up @@ -82,7 +82,6 @@ function isDuplicate(array) {
for (let i = 0; i < array.length; i++) {
//配列の要素をvalueに代入
const value = array[i];

//objに配列要素があったら
if (obj[value]) {
return true;
Expand Down
10 changes: 6 additions & 4 deletions 002_class/003_data.js
Expand Up @@ -121,10 +121,12 @@ class List {
*/
filter(target) {
// TODO:
let result = new List(this.data);
for (let i = 0; i <= result.length; i++) {
if (result[i] === target) {
result.splice(i, 1);
let result = new List(this.data); //Listインスタンスを作成
//自身で作成したsizeを使用
for (let i = 0; i <= result.size; i++) {
//自身で作成したindexを使用
if (result.index(i) === target) {
result.remove(i); //自身で作成したremoveを使用
}
}
return result;
Expand Down
44 changes: 28 additions & 16 deletions 003_practice/001_easy.js
Expand Up @@ -12,7 +12,7 @@

function length(str) {
let ans = 0;
for (let i = 1; i <= str.length; i++) {
while (str[ans]) {
ans++;
}
return ans;
Expand All @@ -30,9 +30,12 @@ function length(str) {
*
*/
function reverse(str) {
let strArray = [...str];
strArray.reverse();
return strArray.join("");
let newStr = "";
//後ろ側から取り出して並べる
for (let i = str.length - 1; i >= 0; i--) {
newStr += str[i];
}
return newStr;
}

/**
Expand All @@ -48,7 +51,12 @@ function reverse(str) {
*/

function findIndex(str, char) {
return str.indexOf(char);
for (let i = 0; i < str.length - 1; i++) {
if (str[i] === char) {
return i;
}
}
return -1;
}

/**
Expand Down Expand Up @@ -103,13 +111,13 @@ function sum(array) {
function average(array) {
if (array.length === 0) {
return 0;
} else {
const sum = array.reduce((pre, current) => {
return pre + current;
}, 0);
const average = sum / array.length;
return Math.floor(average);
}

let average = 0;
for (let i = 0; i < array.length; i++) {
average += array[i];
}
return Math.floor(average / array.length);
}

/**
Expand All @@ -125,7 +133,7 @@ function average(array) {
*/

function concat(a, b) {
return a.concat(b);
return [...a, ...b];
}

/**
Expand All @@ -141,7 +149,11 @@ function concat(a, b) {
*/

function size(array) {
return array.length;
let ans = 0;
for (let value of array) {
ans++;
}
return ans;
}

/**
Expand Down Expand Up @@ -200,13 +212,13 @@ function seq(num) {
*/

function omitSeq(num) {
let ans = 0;
let ans = 1;
let result = [];
while (ans <= num) {
result.push(ans);
ans += 1;
ans += 2;
}
return result.filter((value) => value % 2 == 1);
return result;
}

/**
Expand Down
71 changes: 28 additions & 43 deletions 003_practice/002_medium.js
Expand Up @@ -33,19 +33,20 @@ function rotate(str, num) {
*
*/
function removeVowels(str) {
let strArray = [...str];
strArray.forEach((value) => {
let newStr = "";

for (let i = 0; i < str.length; i++) {
if (
value === "a" ||
value === "e" ||
value === "i" ||
value === "o" ||
value === "u"
str[i] !== "a" &&
str[i] !== "e" &&
str[i] !== "i" &&
str[i] !== "o" &&
str[i] !== "u"
) {
strArray.splice(strArray.indexOf(value), 1, "");
newStr += str[i];
}
});
return strArray.join("");
}
return newStr;
}

/**
Expand Down Expand Up @@ -79,14 +80,12 @@ function countStr(s1, s2) {
*/

function isPalindrome(str) {
const strArray = [...str];
const strReverse = [...str].reverse();

if (strArray.toString() === strReverse.toString()) {
return true;
} else {
return false;
for (let i = 0; i < str.length / 2; i++) {
if (str[i] !== str[str.length - 1 - i]) {
return false;
}
}
return true;
}

/**
Expand All @@ -104,13 +103,16 @@ function isPalindrome(str) {
*
*/
function isPrime(num) {
if (num === 2 || num === 3) {
return true;
} else if (num === 1 || num % 2 === 0 || num % 3 === 0) {
//numが2の場合
if (num === 1) {
return false;
} else {
return true;
}
for (let i = 2; i < num; i++) {
if (num % i === 0) {
return false;
}
}
return true;
}

/**
Expand All @@ -129,32 +131,15 @@ function isPrime(num) {
*
*/
function sumWithout4andNext(array) {
//空の配列をsumArrayを定義
let sumArray = [];
let total = 0;

//array分ループを回す
for (let i = 0; i < array.length; i++) {
//array[1]が4だったら
if (array[i] === 4) {
//sumArrayに0をpush
sumArray.push(0);

//次の要素も4だった場合
if (array[i + 1] === 4) {
//次の要素も0にする
array[i] = 0;
} else {
array[i + 1] = 0;
}
} else {
sumArray.push(array[i]);
if (array[i - 1] !== 4 && array[i] !== 4) {
total += array[i];
}
}

const sum = sumArray.reduce((pre, current) => {
return pre + current;
});
return sum;
return total;
}

module.exports = {
Expand Down

0 comments on commit decb049

Please sign in to comment.