► JS Algorithm/Programmers

[Programmers] JavaScript 알고리즘 | Lv.0 7의 개수

다람트리 2024. 7. 15. 20:14
반응형

🔒 문제 설명

머쓱이는 행운의 숫자 7 가장 좋아합니다. 정수 배열 array 매개변수로 주어질 , 7 있는지 return 하도록 solution 함수를 완성해보세요.


🔒 제한사항  

  • 1 ≤ array의 길이 ≤ 100
  • 0 ≤ array 원소 ≤ 100,000

🔒 입출력 예

array result
[7, 77, 17] 4
[10, 29] 0

🔒 입출력 예 설명

입출력 예 #1

  • [7, 77, 17]에는 7이 4개 있으므로 4를 return 합니다.

입출력 예 #2

  • [10, 29]에는 7 없으므로 0 return 합니다.

 

🔐 solution of mine

Array.prototype.join()

Array.prototype.filter()

  const solution = (array) =>
    console.log([...array.join("")].filter((v) => v === "7").length);

  solution([7, 77, 17]); //output:4
  solution([10, 29]); //output:0

 


 

🔐 solution of others

Array.prototype.join()

String.prototype.split()

const solution = (array) => console.log(array.join("").split(7).length - 1);

solution([7, 77, 17]); //output:4
solution([10, 29]); //output:0

 

🔐 solution of others 

Array.prototype.join()

String.prototype.split()

Array.prototype.filter()

const solution = (array) =>
console.log(
  array
    .join("")
    .split("")
    .filter((v) => v === "7").length
);

solution([7, 77, 17]); //output:4
solution([10, 29]); //output:0

 

🔐 solution of others 

Array.from()

Array.prototype.join()

Array.prototype.filter()

const solution = (array) =>
console.log(Array.from(array.join("")).filter((v) => v === "7").length);

solution([7, 77, 17]); //output:4
solution([10, 29]); //output:0

 


 

반응형