Click here to Skip to main content
15,902,869 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
package main

func NbMonths(startPriceOld, startPriceNew, savingperMonth int, percentLossByMonth float64) [2]int {
	spo := startPriceOld
	spn := startPriceNew
	sm := savingperMonth
	pl := percentLossByMonth

	x := 100
	c := 0.0
	diff := spn - sv
	if diff > 0 {
		c=c+1
		if (c % 2 !==0) {
			spn = spn * ((100 - pl) / 100)
			spo = spo * ((100 - pl) / 100)
			sv := sv + sm + spo
		} else {
			pl = pl + 0.5
			spn = spn * ((100 - pl) / 100)
			spo = spo * ((100 - pl) / 100)
			sv := sv + sm + spo

		}
		countinue
	}
	return [2]int{c, diff}

}
func main() {
	nbMonths(2000, 8000, 1000, 1.5)
}


What I have tried:

i have tried everything it has syntax error on line 14, 15and 18.
Posted
Updated 25-May-21 5:43am
Comments
Greg Utas 25-May-21 11:41am    
I don't know Go, but "countinue" should probably be "continue". It also looks like := is used to declare/initialize, and = is used for assignment, in which case diff := spn - sv and sv := sv + ... will be errors because sv isn't defined yet.

There's a number of issues here: Firstly you have if ( c % 2 !==0 ), which probably wants to be if ( c %2 != 0 ). But even correcting that and pushing your code through the Go Playground The Go Playground[^] we get a number of errors:
./prog.go:11:16: undefined: sv
./prog.go:14:7: invalid operation: c % 2 (operator % not defined on float64)
./prog.go:15:14: invalid operation: spn * ((100 - pl) / 100) (mismatched types int and float64)
./prog.go:16:14: invalid operation: spo * ((100 - pl) / 100) (mismatched types int and float64)
./prog.go:17:10: undefined: sv
./prog.go:20:14: invalid operation: spn * ((100 - pl) / 100) (mismatched types int and float64)
./prog.go:21:14: invalid operation: spo * ((100 - pl) / 100) (mismatched types int and float64)
./prog.go:22:10: undefined: sv
./prog.go:25:3: undefined: countinue
./prog.go:27:16: cannot use c (type float64) as type int in array literal
./prog.go:27:16: too many errors
You should review the error messages and try to address them. If you're having trouble understanding what the error messages mean, or don't know what to do about them, you can post again.
 
Share this answer
 
Go has two basic assignment operators "=" and ":=".
The first is a straight assignment, which requires that the variable on the left hand side already exists.
The second declares the variable, and gives it a value.

If you use the first form without declaring the variable first, you will get an "undefined: <variable name>" compiler error.

So this:
Go
diff := spn - sv
fails because sv is undefined.

And the Go "is not equal to" operation is not "!==" but "!=" so change this line as well:
Go
if (c % 2 !==0) {
But ... modulus is not defined for floating point values, so you'll get an error for that as well ...
How To Convert Data Types in Go | DigitalOcean[^]

And so on. Read the error message, look at the line, it should be pretty obvious! Fix it and compile again. Repeat until you get a clean compile!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900