questions - jer-g123/the_jer_repo GitHub Wiki
would this code work:
package main
import(“fmt”)
func checkArray(options, []string) map[string]bool{
seeker := make(map[string]bool)
seeker[“a”] = false
seeker[“c”] = false
for _,option := range options{
for k, v := range seeker{
if k == result{
seeker[k] = true
}
return seeker
}
}
}
func main(){
options := [“a”, “b”, “c”, “d”]
check := checkArray(vals)
I want to do the following:
- check array for presence or absence of “a” and “b”
- create a map. set key to search term and value to false
- write a for-loop over the array
- write a for-loop over the map
- if key == search term, change value from false to true
- if search term is not present, value is still false
— return missing search term and print
I have the following map:
m[“a”] = true
m[“b”] = false
If ANY of the values are false, I would like to terminate the program using os.Exit(1) and provide a print statement for the user fmt.Printf(“key: %v not found, ending program”, k). If all values in the map are true, then I want a print statement to say fmt.Printf(“all found”). I tried setting the the bools to a numeric value by initializing a variable to 0 and iterating by 1 if a false statement was found:
i := 0
for k, v := range myVals{
if !v{
i++
continue}
if i==0{
print line success}else{
print line failure, end program}
however, this is not working.
Moving the check for `i == 1` inside your loop will not resolve the issue because the core problem lies in when and how you decide to exit the program or print messages. Your original logic checks `i` during each iteration and tries to determine if a failure message should be printed or if the program should exit, without having completed the examination of all the entries in the map.
The key is to complete iterating over all entries in the map to make sure you’ve checked every value before deciding on the next step. The logic to exit the program or print a success/failure message should come after the loop has finished.
Here’s a revised understanding of how to structure your code logically:
1. Iterate through all key-value pairs in the map.
2. If a value is false (`!v`), print the failure message and exit the program immediately because you’ve met the condition to terminate (any value is false).
3. If the loop completes without finding any false values, it means all values are true, so you can print the success message.
Your original attempt at incrementing `i` when a false value is found and then checking `i` to determine if a failure message should be printed or the program should exit, is a step in the right direction but applied incorrectly. You were trying to use `i` to keep track of whether any false values were found, but you need to perform the check after fully iterating through the map, or you can immediately terminate upon finding a false value.
Here’s a simplified version that directly addresses the problem without needing an intermediary counter like `i`:
for k, v := range myVals {
if !v {
fmt.Printf("key: %v not found, ending program\n", k)
os.Exit(1) // This exits the program immediately upon finding any false value.
}
}
// If the loop completes without exiting, all values are true.
fmt.Println("all found")This approach is more straightforward and efficiently achieves what you’re trying to do.