[해결로 표시] 합계 첫 번째 마지막 - JavaScript 작업

  

3
주제 스타터

Write a JavaScript function that calculates and prints the sum of the first and the last elements in an array.
The input comes as array of string elements holding numbers.

예시:

입력:
['20', '30', '40']

출력:
60


입력:
['5', '10']

출력:
15

The output is the return value of your function.

1개 답글
2

My solution 1:

function sumFirstLast(arr) {
    let n1 = Number(arr[0]);
    let n2 = Number(arr[arr.length - 1]);
    console.log(n1 + n2);
}
 
sumFirstLast(['20', '30', '40']);

My solution 2:

function sum(array) {
    return Number(array[0]) + Number(array[array.length - 1]);
}
 
console.log(sum(['15', '55']));
공유: