1

I'm doing a prework for a course I'm going to attend and I have to complete some ex. I started learning JS last week so I'm a newbie and in this ex I don't have to use any method that allows me to calculate the sum. I only have to use logic. (for loops-functions-switch)

This is the ex: Define a function that will receive an array as an argument and calculate a sum of all its elements. For example, if we pass the following array to that function: const prices = [5, 7.99, 9.99, 0.99, 21], it should return 44.97 as output. How would you concatenate $ sign in front of the sum?

I don't really know how to do that sum, in fact the code I've written is totally wrong but I'd like to understand the logic behind.

function calculator (someArray) {
  let result;
  for(let i=0; i<= someArray.length; i++) {
    console.log(someArray[i]);
  }
  result=someArray[i] + someArray[i+=1];
}

const prices = [5, 7.99, 9.99, 0.99, 21];

calculator(prices);

I've now changed the code but it only prints the first 2 elements of the array:

function calculator (someArray) {
  let result;
  for(let i = 0; i<= someArray.length; i++) {
    result = someArray[i] + (someArray[i+=1]);
    console.log(result);
    return result;
  }
}

const prices = [5, 7.99, 9.99, 0.99, 21];

calculator(prices);
3
  • Your code is not working, there is a mismatched parenthesis and you didn't return the result in calculator function. Commented Sep 11, 2020 at 9:29
  • Great start. Not only you have coded something but you asked the question very well. Regarding the code itself: you have an offset-by-one error (actually 2) , which is not executed because of your premature return. But this is normal, just keep going (y) Commented Sep 11, 2020 at 9:50
  • you are going outside of the array twice: once with "<=" and the second time(s) with "i+1" - I think... The for loop increments i, so there is no need to increment inside of it. See my answer for a few ways of doing sum... Commented Sep 11, 2020 at 10:17

4 Answers 4

1

This is the ex: Define a function that will receive an array as an argument and calculate a sum of all its elements. For example, if we pass the following array to that function: const prices = [5, 7.99, 9.99, 0.99, 21], it should return 44.97 as output. How would you concatenate $ sign in front of the sum?

Javascript has a nice feature: reduce

function calculator (someArray){
  return someArray.reduce((howMuchSoFar,currentElementOfTheArray) => {
    howMuchSoFar = howMuchSoFar + currentElementOfTheArray;
    return howMuchSoFar;
  });
}

const prices = [5, 7.99, 9.99, 0.99, 21];

console.log("$"+calculator(prices));

(intentionally verbose)

Going back to your original code (a classic for loop)

  • arrays go from 0 to length-1, control that
  • whathever[i+=1] is actually doing two things: i=i+1 and bringing whatever[i] , it is better going step by step for now, and inside the loop you're already doing i++ so this won't be good
  • return is for the whole function, not for each step of a classic for. If needed, go with break, but please understand firstly the while loop

function calculator (someArray){
  let result=0;
  for( let i = 0; i< someArray.length; i++){  
    result = result + someArray[i];
  }
  return result;
}

const prices = [5, 7.99, 9.99, 0.99, 21];

console.log("$"+calculator(prices));

Sign up to request clarification or add additional context in comments.

Comments

1

function calculator1(someArray) {
  let result=0;
  for(let i=0; i<someArray.length; i++) {
    result=result+someArray[i];
  }
  return result;
}

function calculator2(someArray) {
  let result=0;
  for(let i=0; i<someArray.length; i++) {
    result+=someArray[i];
  }
  return result;
}

function calculator3(someArray) {
  return someArray.reduce((accumulator, currentValue)=>accumulator+currentValue,0);
}

const prices=[5, 7.99, 9.99, 0.99, 21];

console.log("calculator1: "+calculator1(prices));
console.log("calculator2: "+calculator2(prices));
console.log("calculator3: "+calculator3(prices));

let sumWithDollarSign="$"+calculator3(prices);
console.log("sumWithDollarSign: "+sumWithDollarSign);

calculator1 is straight-forward JavaScript.
calculator2 uses += which means: add the right side to the left.
calculator3 uses Array.reduce with accumulator starting with value 0, iterating over all items in Array - each is in currentValue, and adding to accumulator and returning it at the end.

"$"+someNumber concatenates the two. Enough that one of the 2 (or more) is a String to return a String.

Comments

0

Syntax error

replace this

     result = someArray[i] + (someArray[i+=1];
   

with

     result = someArray[i] + (someArray[i+=1]);

Comments

0

This is dirty but works:

const prices = [5, 7.99, 9.99, 0.99, 21];

console.log( eval( prices.toString().replace(/,/g,'+' ) ) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.