0

I have been trying to write a function, where a for loop goes over an array with three numbers (bills), multiplies each array element depending on its value, and then returns the new array(tips), containing the multiplied values. I tried doing it with a for loop, however, the returned array only contains element number 2, while elements 0 and 1 return empty. Where am I going wrong? Thanks a lot for your help!

var bills = [10, 20, 40];

function tipCalc(arr) {
  var tips = [];
  for (i = 0; i < arr.length; i++) {
    if (arr[i] < 10) {
      tips[i] = arr[i] * 0.6;
    } else if (arr[i] > 10 && arr[i] < 20) {
      tips[i] = arr[i] * 0.5;
    } else if (arr[i] > 30) {
      tips[i] = arr[i] * 0.2;
    }
  }
  return tips;
}

console.log(tipCalc(bills)) 

5 Answers 5

4

You're not covering all cases.

if (arr[i] < 10) {
  tips[i] = arr[i] * 0.6;
} else if (arr[i] > 10 && arr[i] < 20) {
  tips[i] = arr[i] * 0.5;
} else if (arr[i] > 30) {
  tips[i] = arr[i] * 0.2;
}

What if the item is exactly 10? What if the item is between 20 and 30? That will not be caught in any of the if-statements you've written here, which is probably why you're missing some elements in the returned array.

To see which one, you could write this:

if (arr[i] < 10) {
  tips[i] = arr[i] * 0.6;
} else if (arr[i] > 10 && arr[i] < 20) {
  tips[i] = arr[i] * 0.5;
} else if (arr[i] > 30) {
  tips[i] = arr[i] * 0.2;
} else {
  console.log('Oops! Unhandled case!', arr[i]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

error is you conditions dont handle arr[i] === 10 , you are handling <10 and >10 but not ===10, same with 20

try this

var bills = [10, 20, 40];

function tipCalc(arr) {
  var tips = [];
  for (i = 0; i < arr.length; i++) {
    if (arr[i] <= 10) {
      tips[i] = arr[i] * 0.6;
    } else if (arr[i] > 10 && arr[i] <= 20) {
      tips[i] = arr[i] * 0.5;
    } else if (arr[i] > 30) {
      tips[i] = arr[i] * 0.2;
    }
  }
  return tips;
}

console.log(tipCalc(bills)) 

1 Comment

Please highlight your changes and explain your answer instead of only posting code. That way, it's more useful for future visitors (and for the OP).
0

There is nothing wrong with your code you have just missed some conditions for 10 20

var bills = [10, 20, 40];

function tipCalc(arr) {
var tips = [];
for (i = 0; i < arr.length; i++) {
if (arr[i] < 10) {
  tips[i] = arr[i] * 0.6;
} else if (arr[i] >= 10 && arr[i] <= 20) {
  tips[i] = arr[i] * 0.5;
} else if (arr[i] > 30) {
  tips[i] = arr[i] * 0.2;
}
}
return tips;
}

console.log(tipCalc(bills)) 

Comments

0

You are not considering the case when the value is equal to 10 or 20

var bills = [10, 20, 40];

function tipCalc(arr) {
  var tips = [];
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] <= 10) { //changed here
      tips[i] = arr[i] * 0.6;
    } else if (arr[i] > 10 && arr[i] <= 20) { //changed here
      tips[i] = arr[i] * 0.5;
    } else if (arr[i] > 30) {
      tips[i] = arr[i] * 0.2;
    }
  }
  return tips;
}

console.log(tipCalc(bills))

If you wish to avoid the if..else and local array variable you can use array map function

var bills = [10, 20, 40];

function tipCalc(arr) {
  return arr.map(function(item) {
    let m = 10 >= item ? 0.6 * item : 10 < item && 20 >= item ? 0.5 * item : 30 < item && (0.2 * item);
    return m
  })
}

console.log(tipCalc(bills))

1 Comment

...or anything between 20 and 30, for that matter.
0

var bills = [10, 20, 40];

function tipCalc(arr) {
  return arr.map(function(v){
  	return v <= 10 ? v * 0.6 : (v > 10 && v <= 20 ? v * 0.5 : v * 0.2);
  });

}

console.log(tipCalc(bills)) 

2 Comments

This works, but when I tried adding v> 30? v*0.2 to the last part of the ternary statement, instead if just : v * 0.2, the code stopped working. Could you explain why?
@VladKhol Replace with this, although both output will be same. return v <= 10 ? v * 0.6 : (v > 10 && v <= 20 ? v * 0.5 : ( v > 30 ? v * 0.2 : v * 0.1 ));

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.