Good morning!,everyone..having some difficulties adding total costs in a for loop statement


#1

var totalPhoneBill = function(string){
var data = string.split(', ');
//console.log(data);
var sms = []
var call = []
var smsCost = 0.65
var callCost = 2.75
for(var i = 0;i<data.length;i++){
if(data[i]=='sms'){
sms.push(data[i]);
}
if(data[i]=='call'){
call.push(data[i])
}
}
//console.log(sms.length);
var totalCost = smsCost.length * callCost.length;
console.log(totalCost);
return totalCost
}
totalPhoneBill('call, sms, call, sms, sms')


Replace this line with your code.


#2

Take a look at this line:

smsCost.length * callCost.length

You wanna calculate the totalCost
How much calls did your customer (call.length) with the price of 2.75 (call)

Same for sms
How much sms send your customer (sms.length) with the price of 0.65 (sms)

var totalCost = (x*y) + (a*b);

#3

What to we have? What do we want to do with it?


#4

I thanks guys that was very helpful.but i actually got to the answer lol..
I tried several steps and suddenly i got to itvar totalCost = smsCost*'3' + callCost*'2';


#5

More dynamic

var totalCost = (smsCost* sms.length) + (callCost*call.length);

#6

yip thanks that worked..i have another complication if u don't mind..need to find the longestWord and the wordsLength.Given a sentence write some functions (function names in brackets) that can:

Find the longest word (longestWord);
find the shortest word (shortestWord);
find the sum of all the word lengths, in a sentence. (wordLengths).

not sure how to approach this


#7

Use split to split the string.
You need a for loop to loop threw the created array.
In the loop put a if statement that compares the longest word length (stored in the first round from your loop)) with the actual word in the array :slight_smile:


#8

var longestWord = function(string){
var data = string.split(',')
var longestWord = [collaboration]
for(var i = 0;i < data.length;i++){
if(data[i] = longestWord.length){
longestWord.push(data[i])


#9

hi! its me again lol..need to subtract this (callCost * call.length) + (dataCost * data.length) + (smsCost * sms.length)
from 50.. if anyone could please help..sorry for being so disturbing..how should i structure that


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.